问题描述
如果我快速点击我的Android应用中的按钮,则它背后的代码似乎运行了两次.如果我两次单击菜单按钮,则必须启动onclick的活动将开始两次,并且必须退出两次.
If i click fast to my button in my Android app, it seems that code behind it runs twice.If i click my menu button twice the activity that has to be launch onclick just starts twice and i have to quit from it twice.
这真的很烦人,因为如果我单击菜单按钮的速度太快,我可以在后台加载一大堆活动,并且必须逐个退出,所以这显然是我想要的应用程序的错误状态解决这个问题.
This is really annoying because if i click too fast for menu buttons i can load up a whole bunch of activities in the background and i must quit them one by one, so this is clearly a buggy state of my app i want to fix this.
该问题我该怎么办?
我使用简单的onClickListeners和Buttons
I use simple onClickListeners and Buttons
关于答案和评论,我的菜单按钮如下所示:
Regarding to answers and comments my menu buttons look like this:
top20Button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
favButton.setClickable(false);
nearButton.setClickable(false);
highlightedButton.setClickable(false);
top20Button.setClickable(false);
Intent i = new Intent();
i.putExtra("showDialog", false);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(Search.this, Top20.class);
startActivity(i);
finish();
}
});
所有更正之后,其仍然相同:S当我像疯子一样单击时,历史记录堆栈上有多个活动,因此我必须退出多次.
After all this correction its still the same :SWhen i click like a mad person multiple activites are on the history stack and i must quit multiple times.
有什么建议吗?我在做什么错了?
Any suggestions ? What m i doing wrong?
推荐答案
您可以使用以下代码: btn.setEnabled(false);
You can use following code: btn.setEnabled(false);
btn.setOnclickListener(new View.onClickListener(){
public void onClick(View v) {
btn.setEnabled(false);
}
});
这篇关于如何通过在Android中快速单击两次按钮来防止运行双重代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!