[Android-解决方案]在android中同时单击两个按钮 [英] [Android - Solution]Click two button in a same time in android

查看:134
本文介绍了[Android-解决方案]在android中同时单击两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android代码中遇到问题.我在片段中有两个按钮:

I have a problem in Android code. I have two button in an fragment:

Button buttonOne = (Button) findViewById(...);
Button buttonTwo = (Button) findViewById(...);

buttonOne将显示一个对话框,而buttonTwo将更改我的片段.当我同时单击buttonOne和buttonTwo(在几秒钟内在buttonTwo之前单击buttonOne)时,显示了一个对话框,但片段已更改(因为单击buttonOne之后,当对话框尚未显示时,接下来单击buttonTwo并进行更改片段)现在,当我单击对话框中的按钮并更改某些内容时,我的应用程序崩溃了.

buttonOne will show a dialog and buttonTwo will change my fragment. When i click both buttonOne and buttonTwo in a same time (buttonOne clicked before buttonTwo in a few second), a dialog showed but the fragment had changed (because after buttonOne is clicked, when the dialog's not showed yet, buttonTwo is clicked next and change the fragment) Now, when i click a button in the dialog and change something, my app crashed.

这不是正常情况,但是我想知道如何在单击buttonOne时如何防止buttonTwo被点击?

It's not a normal case but i wonder how i can prevent buttonTwo is clicked when buttonOne was clicked?

推荐答案

您可以创建一个用作阈值的计时器,然后再次单击另一个按钮.

You can create a timer which will act as a threshold before you can click another button again.

示例:

       @Override
public void onClick(View v) {
    // Preventing multiple clicks, using threshold of 1 second
    if (SystemClock.elapsedRealtime() - mLastClickTime < 500) {
        return;
          }
    mLastClickTime = SystemClock.elapsedRealtime();

    ///YOUR BUTTON CLICK HERE
}

因此,单击另一个按钮之前,它的阈值为500毫秒

So it will have a threshold of 500 millisecond before you can click another button

这篇关于[Android-解决方案]在android中同时单击两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆