我在OnTouchListners中没有收到按钮释放的提示消息

huangapple go评论63阅读模式
英文:

Im not Getting the Button Release Toast in OnTouchListners

问题

问题是我没有收到按钮释放的提示
我在xml中有一个简单的视图在上面执行onTouch操作

hidenBtn.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            firstTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "按下", Toast.LENGTH_SHORT).show();
        } else if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL) {
            Toast.makeText(MainActivity.this, "释放", Toast.LENGTH_SHORT).show();
            secondTime = System.currentTimeMillis();
            if (secondTime - firstTime >= 5000) {
                // 在这里执行你的操作,prev和curr是类中的字段
                ShowDialog();
            } else {
                firstTime = 0;
                secondTime = 0;
            }
        }
        // TODO Auto-generated method stub
        return false;
    }
});
英文:

The issue is im not getting the Button release Toast.
i've a simple view in xml on which im performing onTouch.

hidenBtn.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getActionMasked();
                    if (action == MotionEvent.ACTION_DOWN) {
                        firstTime=System.currentTimeMillis();
                        Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
    
                    } else if (action == MotionEvent.ACTION_UP
                            || action == MotionEvent.ACTION_CANCEL) {
                        Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
                        secondTime=System.currentTimeMillis();
                        if(secondTime-firstTime>=5000){
                            //do your actions here,prev,curr are fields in a class
                            ShowDialog();
                        }
                        else{
                            firstTime=0;
                            secondTime=0;
                        }
                    }
                    // TODO Auto-generated method stub
                    return false;
                }
            });

答案1

得分: 0

将您的OnTouchListener更改如下:

hidenBtn.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
            firstTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "按下", Toast.LENGTH_SHORT).show();
            return true;
        } else if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL) {
            Toast.makeText(MainActivity.this, "释放", Toast.LENGTH_SHORT).show();
            secondTime = System.currentTimeMillis();
            if (secondTime - firstTime >= 5000) {
                // 在这里执行您的操作,prev和curr是类中的字段
                ShowDialog();
            } else {
                firstTime = 0;
                secondTime = 0;
            }
        }
        // TODO: 自动生成的方法存根
        return false;
    }
});

您在ACTION_DOWN时返回的是false,这意味着您没有消耗触摸事件,因此不会触发同一事件的其他步骤。因此,您需要在ACTION_DOWN中返回true,这样您就可以拦截ACTION_UP

英文:

Change your OnTouchListener like this:

hidenBtn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();
            if (action == MotionEvent.ACTION_DOWN) {
                firstTime=System.currentTimeMillis();
                Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
                return true;
            } else if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_CANCEL) {
                Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
                secondTime=System.currentTimeMillis();
                if(secondTime-firstTime>=5000){
                    //do your actions here,prev,curr are fields in a class
                    ShowDialog();
                }
                else{
                    firstTime=0;
                    secondTime=0;
                }
            }
            // TODO Auto-generated method stub
            return false;
        }
    });

You are returning false at ACTION_DOWN which means you are not consuming your touch event, so no other steps of the same event are triggered. Therefore you need to return true in the ACTION_DOWN so you can intercept ACTION_UP.

huangapple
  • 本文由 发表于 2020年10月19日 15:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64422566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定