Enable double-click (Multi-click) events in Android

Source: Internet
Author: User

To implement a double-click, you need to save the time of the first click, use the variable, and then compare it with the time of the second click to see if the time interval is within the time you set (such as 500ms).

?
1234567891011121314151617181920212223242526272829 long firstClickTime = 0;long secondClickTime = 0;public void click1(View view) {    if (firstClickTime > 0) {        secondClickTime = SystemClock.uptimeMillis();        if (secondClickTime - firstClickTime < 500) {            Toast.makeText(this, "第一种双击方式", 0).show();        }        firstClickTime = 0;        return ;    }    firstClickTime = SystemClock.uptimeMillis();        new Thread(new Runnable() {                @Override        public void run() {            //            try {                Thread.sleep(500);                firstClickTime = 0;            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                    }    }).start();}

Double-click events (multi-click events) have two important issues to consider: the time of the first click, the last (how to know the "last") Click Time, and after one click, wait for a period of time (such as 2s), double-click (Multi-click) how to ensure that the correct response. The above code solves both problems, but is inefficient and complex. It is through a variable to store the time of the first click, by judging the size of the variable to determine whether it is a second click, solve the first problem; plus the sleep method of the child thread, if there is no second click in the 500ms, the variable is reset, which solves the second problem. However, this way to solve the double-click event is not bad, then three hit, four hit ... How did it work out?

Second, store variables in a different way?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768 <pre name="code" class="java">  List<long> times = new ArrayList<long>();    public void click2(View view) {        times.add(SystemClock.uptimeMillis());        if (times.size() == 2) {            //已经完成了一次双击,list可以清空了            if (times.get(times.size()-1)-times.get(0) < 500) {                times.clear();                Toast.makeText(this, "第二种双击方式", 0).show();            } else {                //这种情况下,第一次点击的时间已经没有用处了,第二次就是“第一次”                times.remove(0);            }        }    }</long></long></pre><br><br><pre class="brush:java;"></pre>  在第二种方式中,使用List存放点击时的时间。现在来分析一下这个实现的原理:1、如何判断是第二次点击?  通过链表的长度,每次点击list的长度加1,当为2时,表示点击了两次。2、如何消除间隔一段时间的点击事件的影响?  如果是正常的双击,点击两次就完成一次“轮回”,之前保存的时间数据已经使用完毕了,需要清除掉,具体操作就是将List清空。而如果是点击一次后,第二次点击相隔时间较长,那个第一次点击的时间已经没有用处了,就直接将第二次点击视为“第一次”点击,具体而言就是去掉第1次的点击事件。<br>  这个方法,比起第一种方法效率要好得多,而且非常容易扩展到三击、四击……事件。比如:<pre class="brush:java;"><pre name="code" class="java"> List<long> times = new ArrayList<long>();    public void click2(View view) {        times.add(SystemClock.uptimeMillis());        if (times.size() == 3) {            if (times.get(times.size()-1)-times.get(0) < 500) {                times.clear();                Toast.makeText(this, "三击方式", 0).show();            } else {                times.remove(0);            }        }    }</long></long></pre><br><br><pre class="brush:java;"></pre>改变的只是判断条件。  下面是谷歌所写的三击方法,我改写成了双击的方法<pre class="brush:java;"/**     * 双击事件、多击事件     */    //存储时间的数组    long[] mHits = new long[2];    public void doubleClick() {        // 双击事件响应        /**         * arraycopy,拷贝数组          * src 要拷贝的源数组         * srcPos 源数组开始拷贝的下标位置         * dst 目标数组         * dstPos 开始存放的下标位置         * length 要拷贝的长度(元素的个数)         *         */        //实现数组的移位操作,点击一次,左移一位,末尾补上当前开机时间(cpu的时间)        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);        mHits[mHits.length - 1] = SystemClock.uptimeMillis();        //双击事件的时间间隔500ms        if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {            //双击后具体的操作            //do        }    }</pre>  非常简洁,思想差不多,不过谷歌工程师是利用数组移位操作来消除第二个问题的影响的。而要实现多击事件,只需修改数组长度即可。<br> <br>                        </pre>

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

Enable double-click (Multi-click) events in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.