gpt4 book ai didi

android: Activity 使用 OnTouchListener() 启动 2 次

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:08 24 4
gpt4 key购买 nike

我有一个用于更长 pressed_state 按钮的运动类。它真的很好用!

但是有人知道为什么在此代码块中我的 Activity 启动了 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()

最佳答案

只有在操作为 DOWN 时才应该激活它;可能有一个 MOVEUP在此之后的操作,这将再次激活它。

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()

关于android: Activity 使用 OnTouchListener() 启动 2 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com