问题描述
当我增加了听众的OnTouchListener一景,它没有注册。这里是我的code:
When I am adding a listener 'OnTouchListener' to a View, it doesn't register. Here is my code:
GUI gui;
boolean guis = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gui = new GUI(getBaseContext());
gui.setOnTouchListener(this);
setContentView(gui);
}
当我这样做setOnTouchListener(),我把'这个'作为一个参数..假如那是什么东西?
When I do setOnTouchListener(), I put 'this' as a parameter.. Should that be something else?
我让GUI类实现OnTouchListener并增加了一个OnTouch方法...
但我把
I let the GUI class implement OnTouchListener and adds a OnTouch method...But I put
Log.w("AA","Hello")
在OnTouch方法,但它不记录都没有。
In the OnTouch method, yet it doesn't log that at all.
推荐答案
您可以执行以下操作
public class MainActivity extends Activity implements OnTouchListener{
GUI gui;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gui = new GUI(MainActivity.this);
setContentView(gui);
gui.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.w("AA","Hello")
return true;
}
或者你也可以覆盖onTouch在GUI视图
Or you can override the onTouch in your gui view
public class GUI extends View{
Context mcontext;
public MyView(Context context) {
super(context);
mcontext=context;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(mcontext, "View clicked", 1000).show();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// do something
break;
case MotionEvent.ACTION_MOVE:
// do something
break;
case MotionEvent.ACTION_UP:
//do something
break;
}
return true;
}
作为Luksprog评论这个参考对当前上下文。
As Luksprog commented this refer's to the current context.
如果你这样做gui.setOnTouchListener(本);
If you do this gui.setOnTouchListener(this);
您的活动类必须实现OnTouchListener并覆盖onTouch方法。
Your activity class must implement OnTouchListener and override onTouch method.
您也可以在自定义视图覆盖onTouch。
You can also Override onTouch in your custom view.
有没有必要在你身上实现OnTouchListener GUI自定义视图类,如果你只是重写onTouch。
There is no need to implement OnTouchListener in you GUI custom view class if you just override onTouch.
这篇关于OnTouchListener在视图 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!