Connection handling. The button cannot be clicked multiple times in a short period of time.
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6694072.html
When you click a button during App creation, if you do not perform the link processing, the button will be clicked multiple times in a short time.
To avoid this situation, we need to customize an abstract class CustomClickListener and follow View. OnClickListener
Abstract class CustomClickListener implements View. onClickListener {private static final int MIN_CLICK_DELAY_TIME = 1000; // you can only click private long lastClickTime = 0 once in 1 second; private int id =-1; @ Override public void onClick (View v) {long currentTime = Calendar. getInstance (). getTimeInMillis (); int mId = v. getId (); if (id! = MId) {id = mId; lastClickTime = currentTime; onNoDoubleClick (v); return;} if (currentTime-lastClickTime> MIN_CLICK_DELAY_TIME) {lastClickTime = currentTime; onNoDoubleClick (v );}} protected abstract void onNoDoubleClick (View v );}
This document uses the Data-Binding framework. So is added under build. gradle (Module: app ).
android { ... dataBinding { enabled = true }}
Write the connection processing into the class to be implemented
Public class MainActivity extends AppCompatActivity {private ActivityMainBinding mBinding; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mBinding = DataBindingUtil. setContentView (this, R. layout. activity_main); mBinding. btn. setOnClickListener (new CustomClickListener () {@ Override protected void onNoDoubleClick (View v) {// TODO: the operation you want to perform }});}}
Layout File
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tnnowu.android.perfectclick.MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </RelativeLayout></layout>