How to avoid multiple button click at same time in android?

前端 未结 12 1850
太阳男子
太阳男子 2020-12-02 09:05

I\'m using two button in view. While clicking two button simultaneously it will goes to different activity at a time. How to avoid this?

I have tried like this, But

相关标签:
12条回答
  • 2020-12-02 09:49

    For Xamarin users, I have created a solution that subclasses the button class:

    using Android.Content;
    using Android.Runtime;
    using Android.Util;
    using Android.Widget;
    using System;
    using System.Threading.Tasks;
    
    namespace MyProject.Droid.CustomWidgets
    {
        public class ButtonSingleClick : Button
        {
            private bool _clicked = false;
            public int _timer = 700;
            public new EventHandler Click;
    
            protected ButtonSingleClick(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
            {
            }
    
            public ButtonSingleClick(Context context) : base(context)
            {
                base.Click += SingleClick;
            }
    
            public ButtonSingleClick(Context context, IAttributeSet attrs) : base(context, attrs)
            {
                base.Click += SingleClick;
            }
    
            public ButtonSingleClick(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
            {
                base.Click += SingleClick;
            }
    
            public ButtonSingleClick(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
            {
                base.Click += SingleClick;
            }
    
            private void SingleClick(object sender, EventArgs e)
            {
                if (!_clicked)
                {
                    _clicked = true;
    
                    Click?.Invoke(this, e);
    
                    Task.Run(async delegate
                    {
                        await Task.Delay(_timer);
                        _clicked = false;
                    });
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 09:55

    For Kotlin users

    object AppUtil {
    
    var mLastClickTime=0L
    
    fun isOpenRecently():Boolean{
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
            return true
        }
        mLastClickTime = SystemClock.elapsedRealtime()
        return false
    }
    }
    

    In your Activity or Fragment or anywhere

    just add this one line condition

     if(isOpenRecently()) return
    

    example:

    fun startHomePage(activity: Activity){
         if(isOpenRecently()) return //this one line enough 
        val intent= Intent(activity,MainActivity::class.java)
        activity.startActivity(intent)
    
    }
    
    0 讨论(0)
  • 2020-12-02 09:57

    The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example:

    // Make your activity class to implement View.OnClickListener
    public class MenuPricipalScreen extends Activity implements View.OnClickListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // setup listeners.
            findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
            findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
            ...
         }
    
        .
        .
        .
    
        // variable to track event time
        private long mLastClickTime = 0;
    
        // View.OnClickListener.onClick method defination
    
        @Override
        public void onClick(View v) {
            // Preventing multiple clicks, using threshold of 1 second
            if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                return;
            }
            mLastClickTime = SystemClock.elapsedRealtime();
    
            // Handle button clicks
            if (v == R.id.imageView2) {
                // Do your stuff.
            } else if (v == R.id.imageView3) {
                // Do your stuff.
            }
            ...
        }
    
        .
        .
        .
    
     }
    
    0 讨论(0)
  • 2020-12-02 09:57

    if you are using kotlin then create extension fun as following:

    fun View.clickWithDebounce(debounceTime: Long = 1200L, action: () -> Unit) {
        this.setOnClickListener(object : View.OnClickListener {
            private var lastClickTime: Long = 0
            override fun onClick(v: View) {
                if (SystemClock.elapsedRealtime() - lastClickTime < debounceTime) return
                else action()
                lastClickTime = SystemClock.elapsedRealtime()
            }
        })
    }
    

    Now on any view just call :

    view.clickWithDebounce{
    ...
    }
    
    0 讨论(0)
  • 2020-12-02 09:59

    first :

    public class ClickValidate {
        public static long lastClickTime;
    
        public static boolean isValid()
        {
            long current=System.currentTimeMillis();
            long def = current - lastClickTime;
            if (def>1000)
            {
                lastClickTime = System.currentTimeMillis();
                return true;
            }
            else
                return false;
        }
    }
    

    Now just call this method everywhere in the body of onCLick method Or wherever you need:

    if (ClickValidate.isValid()){
    
       //your code
    
    }
    
    0 讨论(0)
  • 2020-12-02 10:00

    The simple way to do it in Kotlin is to use:

    //When you need to disable the button
      btn.isEnabled = false
    
    //When you need to enable the button again 
      btn.isEnabled = true
    
    0 讨论(0)
提交回复
热议问题