多次点击
一、彩蛋 1、快速点击3次 2、再次点击一下 |
二、查看系统如何实现多次点击 导入Settings 1、Android version 2、CTRL+H,搜Android version public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) { System.arraycopy(mHits, 1, mHits, 0, mHits.length-1); mHits[mHits.length-1] = SystemClock.uptimeMillis(); if (mHits[0] >= (SystemClock.uptimeMillis()-500)) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("android", com.android.internal.app.PlatLogoActivity.class.getName()); try { startActivity(intent); } catch (Exception e) { Log.e(LOG_TAG, "Unable to start activity " + intent.toString()); } } |
三、系统3击事件源码分析: |
四、案例
package cn.zengfansheng.threeclick; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 1、存时间的数组 long[] mHits = new long[3]; public void threeClick(View view) { // 2、数组拷贝,将mHits的值位置1开始拷贝的mHits从0开始,拷贝mHits.length-1个 // 那么最后一个位置留下了 System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); // 3、mHits最后一个位置赋值为当前系统时间 mHits[mHits.length - 1] = SystemClock.uptimeMillis(); //4、如果mHits第一个元素 if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) { Toast.makeText(getApplicationContext(), mHits.length + "击事件~~~",0).show(); } } /** * 我的四击事件 * @param view */ long[] myHits = new long[4]; public void myFourClick(View view) { // 1、拷贝 System.arraycopy(myHits, 1, mHits, 0, myHits.length - 1); // 2、赋值系统时间给最后一个元素 myHits[myHits.length - 1] = SystemClock.currentThreadTimeMillis(); // 判断 if (myHits[0] >= (myHits[myHits.length - 1] - 600)) { Toast.makeText(getApplicationContext(), myHits.length + "击事件~~~", 0) .show(); } } } |