java Android:防止按钮上的多个 onClick 事件(已禁用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10848157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:49:33  来源:igfitidea点击:

Android: Prevent multiple onClick events on a button (that has been disabled)

javaandroiduser-interface

提问by rluba

A button triggers an action that should only be invoked once. The button is disabled and hidden in the onClick handler before the action is performed:

一个按钮触发一个应该只调用一次的动作。在执行操作之前,按钮被禁用并隐藏在 onClick 处理程序中:

someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}

Even though the button is disabled immediately, it is nonetheless possible to trigger multiple "onClick" events by tapping multiple times very quickly. (i.e. performTaskOnceis called multiple times). Is seems that the onClick events are queued before the the button is actually disabled.

即使按钮立即被禁用,仍然可以通过快速点击多次来触发多个“onClick”事件。(即被performTaskOnce多次调用)。似乎 onClick 事件在按钮实际禁用之前排队。

I could fix the problem by checking in every single onClick handle whether the corresponding button is already disabled but that seems like a hack. Is there any better way to avoid this issue?

我可以通过检查每个 onClick 句柄是否已禁用相应的按钮来解决问题,但这似乎是一个黑客。有没有更好的方法来避免这个问题?

The problem occurs on Android 2.3.6, I cannot reproduce it on Android 4.0.3. But given the rarity of 4.x devices it is not an option to exclude older devices.

问题出现在 Android 2.3.6 上,我无法在 Android 4.0.3 上重现它。但鉴于 4.x 设备的稀有性,不能排除旧设备。

采纳答案by Vipul Shah

You could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.

您可以在单击按钮时将布尔变量设置为 true,并在完成单击处理后将其设置为 false。

This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.

通过这种方式,您可以忽略多次点击,而不必禁用按钮,这可能会避免令人讨厌的按钮闪烁。

boolean processClick=true;
someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(processClick)
         {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
         }
        processClick=false; 
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}

回答by Christopher Perry

In the interest of keeping DRY:

为了保持干燥

// Implementation
public abstract class OneShotClickListener implements View.OnClickListener {
    private boolean hasClicked;

    @Override public final void onClick(View v) {
        if (!hasClicked) {
            onClicked(v);
            hasClicked = true;
        }
    }

    public abstract void onClicked(View v);
}

// Usage example
public class MyActivity extends Activity {
    private View myView;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myView.setOnClickListener(new OneShotClickListener() {
            @Override public void onClicked(View v) {
                // do clicky stuff
            }
        });
    }
}

回答by daesu

Bit late but this might be of use to someone. In my case I am calling another activity so;

有点晚了,但这可能对某人有用。就我而言,我正在调用另一个活动;

Declare a boolean;

声明一个布尔值;

boolean clickable;

In the click listener;

在点击监听器中;

if(clickable){
   // Launch other activity
   clickable = false;
}

Enable when onResume is called;

调用 onResume 时启用;

@Override
public void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
    clickable = true;
}

回答by Pavlo Zorya

You can use RxView(com.jakewharton.rxbinding2.view.RxView) is an extension around RxJava that created by Jake Wharton.

您可以使用RxView( com.jakewharton.rxbinding2.view.RxView) 是由 Jake Wharton 创建的围绕 RxJava 的扩展。

To integrate it to project you should use implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'

要将其集成到项目中,您应该使用 implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'

Simple Java usage:

简单的Java用法:

RxView.clicks(yourButton)
    .sample(500, TimeUnit.MILLISECONDS)
    .subscribe { action() }

In Kotlinyou can create extension function to handle your clicks:

Kotlin您可以创建扩展功能来处理你的浏览次数:

View.singleClick(action: () -> Any) {
    RxView.clicks(this)
        .sample(500, TimeUnit.MILLISECONDS)
        .subscribe { action() }
}

Sample:

样本:

Kotlin

科特林

yourButton.singleClick({
    //do some stuff here
})

Java

爪哇

SingleClickListenerKt.singleClick(yourButton, () -> {
    doSomeStuff();
    return null;
    });

Note:you can use any RxJava operators like debounce, map, first, etc if you wish.

注意:debounce, map, first如果您愿意,您可以使用任何 RxJava 运算符,例如等。

回答by Deepak Swami

declare a varieble and use it as

声明一个变量并将其用作

 boolean boo = false;
 someButton.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 if(boo==false){
    someButton.setEnabled(false);
    someButton.setClickable(false);
    someButton.setVisibility(View.GONE);
    boo = true;
 }


    }
});

by this you prevent multiple clicks on your button

通过这种方式,您可以防止多次点击您的按钮

hope it help

希望它有帮助