How to prevent rapid double click on a button

后端 未结 11 1218
野的像风
野的像风 2021-01-01 19:12

I have looked at the answers here - Android Preventing Double Click On A Button and implemented qezt\'s solution like and I\'ve tried setEnabled(false) like so

相关标签:
11条回答
  • 2021-01-01 19:45

    Suru's answer worked well for me, thanks!

    I'd like to add the following answer for anybody who's using https://github.com/balysv/material-ripple/blob/master/library/src/main/java/com/balysv/materialripple/MaterialRippleLayout.java and is facing this problem -

    app:mrl_rippleDelayClick is true by default. This means, onClick will be executed only after it's finished showing the ripple. Hence setEnabled(false)inside onClick will be executed after a delay because the ripple isn't done executing and in that period you may double click the view.

    Set app:mrl_rippleDelayClick="false" to fix this. This means the call to onClick will happen as soon as the view is clicked, instead of waiting for the ripple to finish showing.

    0 讨论(0)
  • 2021-01-01 19:45

    try to set yourbutton.setClickable(false) like this :

    onclick(){
    yourbutton.setClickable(false) ;
    ButtonLogic();
    }
    
    ButtonLogic(){
    //your button logic
    yourbutton.setClickable(true);
    }
    
    0 讨论(0)
  • 2021-01-01 19:48
    public static void disablefor1sec(final View v) {
            try {
                v.setEnabled(false);
                v.setAlpha((float) 0.5);
                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            v.setEnabled(true);
                            v.setAlpha((float) 1.0);
                        } catch (Exception e) {
                            Log.d("disablefor1sec", " Exception while un hiding the view : " + e.getMessage());
                        }
                    }
                }, 1000);
            } catch (Exception e) {
                Log.d("disablefor1sec", " Exception while hiding the view : " + e.getMessage());
            }
        }
    

    I kept above method in a static file and i will call this method for all the buttonclick like this

        button_or_textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StaticFile.disablefor1sec(button_or_textview);
                // Do Somthing.
            }
        });
    
    0 讨论(0)
  • 2021-01-01 19:54

    I use a function like this in the listener of a button:

    public static long lastClickTime = 0;
    public static final long DOUBLE_CLICK_TIME_DELTA = 500;
    
    public static boolean isDoubleClick(){
        long clickTime = System.currentTimeMillis();
        if(clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            lastClickTime = clickTime;
            return true;
        }
        lastClickTime = clickTime;
        return false;
    }
    
    0 讨论(0)
  • 2021-01-01 19:57
    private static final long MIN_CLICK_INTERVAL = 1000;
    
    private boolean isViewClicked = false;
    
    private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    final public void onClick(View view) {
        if (isViewClicked) {
          return:
        }
        // place your onClick logic here
        isViewClicked = true;
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                isViewClicked = false;
            }
        }, MIN_CLICK_INTERVAL);
    }
    

    }

    0 讨论(0)
提交回复
热议问题