简体   繁体   English

如何解决android中双击按钮问题?

[英]How to resolve double tap on Button Issue in android?

Mockup of my Application :我的应用程序模型:

双击模型


Problem :问题 :

When click on button1 it just call Intent of ActivitySecond当单击button1 时,它只是调用ActivitySecond 的Intent

button1.setOnClickListener(this);

public void onClick(View v) {
// TODO Auto-generated method stub
     switch (v.getId()) 
     {
          case R.id.button1:
                Intent intent = new Intent(getApplicationContext(), ActivitySecond.class);
                startActivity(intent);
                break;
          default:
                break;
     }
}

But, on Double tap it open twice ActivitySecond.但是,双击它打开两次ActivitySecond。


HOW TO RESOLVE IT.如何解决。

PLEASE IF ANY SOLUTION THEN SHARE IT.请如果有任何解决方案,然后分享它。

Thank you.谢谢你。

As Gabe Sechan sad:正如 Gabe Sechan 伤心的:

This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click)这可以通过计时器完成(获取他们点击它的时间,保存它,如果他们在 100 毫秒内再次点击它,忽略第二次点击)

Here is an implementation that i used in my project:这是我在我的项目中使用的一个实现:

public abstract class OnOneClickListener implements View.OnClickListener {
    private static final long MIN_CLICK_INTERVAL = 1000; //in millis
    private long lastClickTime = 0;

    @Override
    public final void onClick(View v) {
        long currentTime = SystemClock.elapsedRealtime();
        if (currentTime - lastClickTime > MIN_CLICK_INTERVAL) {
            lastClickTime = currentTime;
            onOneClick(v);
        }
    }

    public abstract void onOneClick(View v);
}

Just use OnOneClickListener instead of OnClickListener and execute your code in onOneClick() method.只需使用OnOneClickListener而不是OnClickListener并在onOneClick()方法中执行您的代码。

The solution with disabling button in onClick() will not work.onClick()禁用按钮的解决方案将不起作用。 Two clicks on a button can be scheduled for execution even before your first onClick() will execute and disable the button.即使在您的第一个onClick()将执行并禁用按钮之前,也可以安排对按钮的两次单击以执行。

This is called debouncing- its a classical problem in hardware and in software.这称为去抖动——这是硬件和软件中的一个经典问题。 There's a couple of tricks you can do, but they all boil down to disabling the button temporarily and re-enabling it later.您可以使用一些技巧,但它们都归结为暂时禁用按钮并稍后重新启用它。 This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click).这可以通过计时器完成(获取他们点击它的时间,保存它,如果他们在 100 毫秒内再次点击它,忽略第二次点击)。 Another way would be to disable the button after onClick and re-enable it when the new Activity finishes via onActivityResult.另一种方法是在 onClick 之后禁用按钮,并在新活动通过 onActivityResult 完成时重新启用它。 Or there's a dozen other ways, pick the easiest for you.或者还有其他十几种方法,选择最适合您的方法。

You can set launchMode of ActivitySecond to singleTop您可以将ActivitySecondsingleTop设置为singleTop

<activity android:name=".ActivitySecond"
            android:launchMode="singleTop"
            >
            ...
</activity>
btn.setOnclickListener(new View.onClickListener(){

          public void onClick(View v) {
                btn.setEnabled(false);

          }
    });

you have to make the setEnabled(false) in onlclick event.您必须在 onlclick 事件中设置 setEnabled(false)。

When I use selector drawable in Buttons and set当我在 Buttons 中使用可绘制选择器并设置时

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

it performs the onClick() event on double click.它在双击时执行 onClick() 事件。 I found it accidentally to be working on android emulator api level 10, Android 2.3.3 Didn't tested on real device.我偶然发现它在 android 模拟器 api 级别 10 上工作,Android 2.3.3 没有在真实设备上测试。 Here is the Complete code.这是完整的代码。

               <Button
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Discover"
                android:id="@+id/Button1"
                android:layout_weight=".5"
                android:layout_margin="0dp"
                android:background="@drawable/btn_nearby"
                android:contentDescription="gjhfjhkjhgkvkjh"
                android:drawableLeft="@drawable/ic_follow"
                android:paddingLeft="20dp"
                android:paddingRight="0dp"
                android:drawablePadding="-10dp"
                android:textSize="16sp"
                android:paddingTop="2.5dp"
                android:paddingBottom="2.5dp"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"/> 

And Java Code和 Java 代码

    @Override
    public void onClick(View view) {
        switch(view.getId()) {

            case R.id.Button1:
                onButton1Click();
                break;

            case R.id.Button2:
                onButton2Click();
                break;

          }
    }

I will just submit a third solution for this.我将为此提交第三个解决方案。 I solved it by adding a boolean which was used to check if the activity (which the button starts) has been started or not.我通过添加一个布尔值来解决它,该布尔值用于检查活动(按钮启动的活动)是否已启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM