How to prevent rapid double click on a button

后端 未结 11 1279
野的像风
野的像风 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: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.
            }
        });
    

提交回复
热议问题