gpt4 book ai didi

Android - 触摸监听器触发了两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:31 26 4
gpt4 key购买 nike

在我的代码中,按钮的 ontouch 监听器被触发了两次。请在下面找到代码。我使用的是 Google API 2.2。

java文件中的代码....

submit_button = (Button)findViewById(R.id.submit);

submit_button .setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1) {
int action=0;
if(action == MotionEvent.ACTION_DOWN)
{

startActivity(new Intent(First_Activity.this, Second_Activity.class));
finish();
}
return true;
}
});

请帮我解决这个问题。

最佳答案

它会触发两次,因为有一个向下事件和一个向上事件。

if 分支中的代码始终执行,因为操作设置为 0(顺便说一下,它是 MotionEvent.ACTION_DOWN 的值)。

int action=0;
if(action == MotionEvent.ACTION_DOWN)

也许您打算改为编写以下代码?

if(arg1.getAction() == MotionEvent.ACTION_DOWN)

但是您真的应该按照 Waqas 的建议使用 OnClickListener。

关于Android - 触摸监听器触发了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9548421/

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