我有一个更长的pressed_state按钮的动作类。它工作得很好!
但是有人知道为什么在这个代码块中我的活动开始了2次吗?当我按下后退按钮时,我要做2次。
感谢您的帮助!
这是我的java代码:
Button MenuBtnStart;
final Handler handlerBtnStart = new Handler();
MenuBtnStart.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
MenuBtnStart.setBackgroundDrawable(getResources().getDrawable(R.drawable.hover));
v.setPressed(true);
handlerBtnStart.postDelayed(new Runnable() {
public void run() {
Intent myIntent = new Intent(TextActivity.this, NextActivity.class);
TextActivity.this.startActivity(myIntent);
v.setPressed(false);
}
}, 900); // end of Handler new Runnable()
return true;
}
}); // end of OnTouchListener()
答案 0 :(得分:3)
如果操作是DOWN
,您应该只是激活它;在此之后可能会有MOVE
或UP
操作,这会在第二次激活它。
final Handler handlerBtnStart = new Handler();
MenuBtnStart.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
MenuBtnStart.setBackgroundDrawable(getResources().getDrawable(R.drawable.hover));
v.setPressed(true);
handlerBtnStart.postDelayed(new Runnable() {
public void run() {
Intent myIntent = new Intent(ThisActivity.this, NextActivity.class);
ThisActivity.this.startActivity(myIntent);
v.setPressed(false);
}
}, 900); // end of Handler new Runnable()
return true;
}
return false;
}
}); // end of OnTouchListener()
答案 1 :(得分:0)
我认为它是在onKeyUp()
和onKeyDown()
上调用的 - 这些是你应该覆盖的方法,而不是onTouch()
。
修改强>
我没有仔细阅读 - 经过长时间的点击,我起初并没有得到它。我曾经收到类似问题的答案。看看:How to access menu button on long click。
答案 2 :(得分:0)
你应该处理ACTION_DOWN事件。
if (event.getAction() == MotionEvent.ACTION_DOWN){
// Do Something
}
return true;