Solution: Set a response interval for each click
Effects: I copy the code to run, I test is normal, do not deal with the original, fast click, there really will be two interface situation.
1. Page jump, create a new FirstActivirty
Remember to register in the list file
<activity android:name="com.example.admin.zkotlin.FirstActivity"/>
2. This new interface needs to be jumped to
import android.support.v7.app.AppCompatActivity import android.os.Bundle class FirstActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_first) } }
3. The main interface has two buttons, one is the normal click jump, the other is the modified jump.
import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import kotlinx.android.synthetic.main.activity_main.* import com.example.admin.zkotlin.NoDoubleClickListener class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button1.setOnClickListener { var intent = Intent(this@MainActivity, FirstActivity::class.java) startActivity(intent) } button2.setOnClickListener(object : NoDoubleClickListener() { override fun onNoDoubleClick(v: View?) { Log.e("TAG", "button2 click") var intent = Intent(this@MainActivity, FirstActivity::class.java) startActivity(intent) } }) } }
4. Optimized click events for global encapsulation and time response judgement
package com.example.admin.zkotlin; import android.os.SystemClock; import android.view.View; /** * User: Gavin * E-mail: GavinChangCN@163.com * Desc: Solve "click" continuous clicks * Date: 2016-12-01 * Time: 17:26 */ public abstract class NoDoubleClickListener implements View.OnClickListener { protected static final String TAG = "NoDoubleClickListener"; private static final int MIN_CLICK_DELAY_TIME = 1000; private long lastClickTime = 0; public abstract void onNoDoubleClick(View v); @Override public void onClick(View v) { long currentTime = SystemClock.uptimeMillis(); if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) { lastClickTime = currentTime; onNoDoubleClick(v); } } }
end