How to prevent the activity from loading twice on pressing the button

后端 未结 19 1995
情深已故
情深已故 2020-12-01 00:15

I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.

I have an activity which loads on click of a butt

19条回答
  •  甜味超标
    2020-12-01 00:28

    // variable to track event time

    private long mLastClickTime = 0;
    

    2.In onClick check that if current time and last click time difference are less than i second then dont't do anything(return) else go for click event

     @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 ur stuff.
             }
                else if (v == R.id.imageView2) {
            // Do ur stuff.
             }
          }
     }
    

提交回复
热议问题