我有自定义画布的主要活动:
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的方法调用qwerty()(例如onSingleTapConfirmed)
这可能吗?如果没有,还有另一种方法吗? 感谢
....编辑.....(更多信息)
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 :(得分:0)
你有两个选择。要么在Activity中实现SimpleOnGestureListener并将其设置为CustomCanvas,要么将Activity传递给CustomCanavas,以便可以从CustomCanvas类中的侦听器调用qwerty()。
更新
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;
}
}
在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);
....
}
}