OnTouchListener不工作的内部活动 [英] OnTouchListener not working inside Activity
问题描述
所以我有一些问题onTouch方法,它没有做任何事情,起初我还以为东西与code wrogn但我这样做,它仍然不工作,当我触摸屏幕。任何人都知道这是什么问题?日Thnx。
so I had some problems with the onTouch method, it didnt do anything, at first i thought something wrogn with the code but then i did this, and it still doesnt work when i touch the screen. Anyone knows what is the problem? Thnx.
{
私人FartPianoView FPV;
{ private FartPianoView fpv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
fpv = new FartPianoView(this);
setContentView(fpv);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
// TODO Auto-generated method stub
float currentX = event.getX();
float currentY = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
finish();
break;
case MotionEvent.ACTION_UP:
finish();
break;
}
return false;
}
}
推荐答案
您需要替换的
public boolean onTouchEvent (MotionEvent e)
在活动的方式
类,你是压倒一切的不是方法( onTouch(...)
的 OnTouchListener
)。那么它应该工作。例如:
method of the Activity
class, not the method you are overriding (onTouch(...)
of the OnTouchListener
). Then it should work. Example:
@Override
public boolean onTouchEvent(MotionEvent e) {
// do your stuff...
return false;
}
这意味着你已经意识到在活动触摸事件
,而不是在查看
。如果你想明确地检测上的查看触摸
通过 OnTouchListener
,需要设置 OnTouchListener
您查看
。
This means you are recognizing touch events on the Activity
, not on the View
. If you want to explicitly detect touches on the View
via OnTouchListener
, you need to set the OnTouchListener
for your View
.
在您的情况:
fpv = new FartPianoView(this);
fpv.setOnTouchListener(this);
这篇关于OnTouchListener不工作的内部活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!