OnTouchListener不工作的内部活动 [英] OnTouchListener not working inside Activity

查看:190
本文介绍了OnTouchListener不工作的内部活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些问题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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆