Android Multiple Click events

Source: Internet
Author: User

Android does not provide double-click or multiple-click events this time, we need to implement them.

Thought: Use the array to record the system time when clicked, if the current click event occurs after 3 clicks and the current system time and the first click event occurs with a difference of less than 500 is considered a continuous click and clicked 3 times.

The use of arrays is a very clever control of the number of clicks, because the default initial value of the array is all 0, each click will be the last element of the array to assign a value, and then all the elements of the element to the left one, each time the first click on the value of the event to move to the left one, if left 3 times mhints[0] must If Mhints[0] is 0 not yet clicked 3 times, if mhints[0] does not equal 0 indicates that the array has moved 3 times and 3 Click events occurred

The next step is to consider whether it is a continuous click, assuming that the system time for each click is the following value.

Array initial: 0 0 0

First time clicks: 0 0 1020

Second time Click 0 1020 1050

Third Click 1020 1050 1070

After the 3rd click, the IF Judgment statement assumes that the current system time is 1075

if (Systemclock.uptimemillis ()-mhints[0]<=500)

This line statement is 1075-1020<=500, 1020 is the time of the first click, the 3rd click after the time 1075 and the first click on the time 1020 of the difference is 3 times the time spent on the click, if the difference is less than 500 we think is a continuous click

Package Com.passion.mobilesafe;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import Android.view.View;
Import Android.widget.TextView;
Import Android.widget.Toast;
/**
* Created by passion on 2015/8/10.
*/
public class Multiclickactivity extends Activity
{
Private TextView Tv_name;
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Tv_name= (TextView) Findviewbyid (r.id.tv_toast_address);
Tv_name.setonclicklistener (New View.onclicklistener ()
{
You need to listen for several clicks the length of the event array is just a few
If you are listening to a double-click event, the array length is 2, and if you want to listen for 3 consecutive click events, the array length is 3 ...
long[] mhints = new long[3];//all initially 0
@Override
public void OnClick (View v)
{
Moves all elements in the mhints array one position to the left
System.arraycopy (mhints, 1, mhints, 0, mhints.length-1);
Get the time the current system has started
Mhints[mhints.length-1] = Systemclock.uptimemillis ();
if (Systemclock.uptimemillis ()-mhints[0]&lt;=500)
Toast.maketext (Multiclickactivity.this, &quot; clicked 3 times &quot;, Toast.length_short). Show ();
}
});
}
}

Android Multiple Click events

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.