英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论