安卓onTouch监听事件

3

我正在使用ontouch事件,但问题是它调用了down事件,但没有调用move事件或UP事件。请检查以下代码:

public class DragNewActivity extends Activity implements OnTouchListener  {

    private float X;
    private float Y;
    private int width;
    private int height;
    private CharSequence s;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.d("D","D");

        TextView tv1 = (TextView)findViewById(R.id.tv1);
        TextView tv2 = (TextView)findViewById(R.id.tv2);
        tv1.setOnTouchListener(this);
        //tv2.setOnTouchListener(this);
        }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        //int action = event.getAction();
        switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            Log.d("DOWN","DOWN");
        break;

        case MotionEvent.ACTION_MOVE:
            Log.d("MOVE","MOVE");
        break;

        case MotionEvent.ACTION_UP:
            Log.d("UP","UP");
            X = event.getRawX();
            Y = event.getRawY();

            Display display = getWindowManager().getDefaultDisplay(); 
            width=display.getWidth()/2;
            height=display.getHeight()/2;
            Log.e("X", X+"");
            Log.e("Y", Y+"");
            Log.e("ScX", width+"");
            Log.e("ScY", height+"");
            if(X>width && Y>height){
                Log.e("SUFI", "Event ho gyuaaaaaaa");
            }

        break;
        }
        return false;
    }
}
3个回答

7
不要从ontouch返回false,返回true以便监听下一个动作事件。

0

我知道这可能有点老了,但我认为更准确的说法是事件按顺序发生:

按下 -> 移动 -> 抬起。

每个事件至少调用一次"OnTouch"方法,并且在移动时多次调用onMove。

因此,如果您从Down返回false,则不会继续执行Move,然后抬起。

如果您从Down返回true,则会继续执行Move。如果Move返回false,则不会继续执行Up,依此类推。


0

你需要返回 true 而不是返回 false。

当你实现 onTouch 方法时,它必须返回 true。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接