2012-01-02 133 views
0

活動的功能我有一個自定義畫布主要活動:呼叫從SimpleOnGestureListener

public void onCreate(Bundle savedInstanceState) { 
     ... 
     CustomCanvas c = new CustomCanvas(this); 
     c.requestFocus(); 
     cll = (LinearLayout)findViewById(R.id.CLL); 
     cll.addView(c); 
    } 

    public void qwerty(String w) { 
     .... 
     TextView abc = (TextView)findViewById(R.id.TextViewabc); 
     abc.setText(w); 
     .... 
    } 

裏面CustomCanvas,我有一個SimpleOnGestureListener GestureDetector。 我想從SimpleOnGestureListener的方法(如onSingleTapConfirmed)調用qwerty()

這可能嗎?如果不是,還有另外一種方式嗎? 感謝

....編輯.....(詳細信息)

GestureDetector是一個對象,我CustomCanvas

public class CustomCanvas extends View { 

    GestureDetector gd; 
    ... 

    public CustomCanvas(final Context context) { 
     super(context); 

     gd = new GestureDetector(getContext(), new SimpleOnGestureListener() { 
      .... 
      // I also use getScrollX() and getScrollY() in some of the methods here 
     }); 
    } 

    .... 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return gd.onTouchEvent(ev); 
    } 
} 
+0

也向我們展示'GestureDetector'。這是在同一班? – 2012-01-02 13:00:26

回答

0

你有兩種選擇。您可以在Activity中實現SimpleOnGestureListener,並將其設置爲CustomCanvas,或者將Activity傳遞給CustomCanavas,以便您可以從CustomCanvas類中的偵聽器調用qWerrty()。

UPDATE

public class CustomCanvas extends View { 

    GestureDetector gd; 
    YourActivity mYourActivity; 
    ... 

    public CustomCanvas(final Context context) { 
     super(context); 

     gd = new GestureDetector(getContext(), new SimpleOnGestureListener() {     
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ 
        // this implementation makes no sense 
        if(mYourActivity != null){ 
        mYourActivity.qwerty(); 
        }  
       }  
     }); 
    } 

    public setActivity(YourActivity activity){ 
     mYourActivity = activity; 
    } 
} 

在你的活動類,你必須將活動傳遞給CustomCanvas。

public class YourActivity { 

    public void onCreate(Bundle savedInstanceState) { 
     ... 
     CustomCanvas c = new CustomCanvas(this); 

     // pass the activity to the canvas 
     c.setActivity(this); 

     c.requestFocus(); 
     cll = (LinearLayout)findViewById(R.id.CLL); 
     cll.addView(c); 
    } 

    public void qwerty(String w) { 
     .... 
     TextView abc = (TextView)findViewById(R.id.TextViewabc); 
     abc.setText(w); 
     .... 
    } 
} 
+0

我該怎麼做? >< – user1126234 2012-01-02 13:52:31

+0

好吧,你是否只實現了CustomCanvas類來覆蓋SimpleOnGestureListener? – Flo 2012-01-02 13:58:49

+0

是的。我重寫了onScroll,onFling,onDown和onSingleTapConfirmed方法 – user1126234 2012-01-02 14:05:17