2012-01-02 167 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