一种高效的android双击退出(可扩展多击)

参考Google,安卓手机中在查看安卓系统版本的地方,三击或者多击会出现彩蛋,可以借鉴其源码进行实现。

//利用数组来存储时间
    long[] mHits = new long[3];
    @Override
    public void onClick(View v) {
        // arraycopy 拷贝数组
        /*  参数解读如下:
         *  src the source array to copy the content.   拷贝哪个数组
         *    srcPos the starting index of the content in src.  从原数组中的哪里开始拷贝
         *    dst the destination array to copy the data into.  拷贝到哪个数组
         *    dstPos the starting index for the copied content in dst.  从目标数组的那个位置开始去写
         *    length the number of elements to be copied.  拷贝的长度
         */
//实现的功能就相当于把mHits从索引1开始的数据向前移动一位(0位数据被覆盖),然后把当前距离时间写入到数组的最后一位
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //获取离开机的时间
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        //单击时间的间隔,以500毫秒为临界值
        if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
            System.out.println("我被三击了。。。");
            //一个三击(双击或多击事件完成),
            //把数组置为空并重写初始化,为下一次三击(双击或多击)做准备
            mHits = null;
            mHits = new long[3];
        }

上面,如果数组长度改为2,那么就是双击了
 

猜你喜欢

转载自blog.csdn.net/feixiangsmile/article/details/81155816