编写布局文件;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.onclick.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="多击事件" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="多击事件" />
</LinearLayout>
java代码;
package com.example.onclick;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
private Button button2;
private long[] mHits = new long[3];
private long firstClick = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:// 谷歌源码编写的多击事件;
// 1原数组要拷贝的数组;(点击的次数)
// 2 原数组拷贝起始位置的索引值;
// 3目标数组(原数组的数据——拷贝——目标数组)
// 4目标数组接收值的起始索引位置;
// 5拷贝的次数(拷贝的次数);
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 拿到的点击事件的时间,赋值给数组的最后一个元素;
if (mHits[mHits.length - 1] - mHits[0] < 500) {// 在500毫秒的时间点击了3次就能响应此次点击事件;
Toast.makeText(this, "点击了三次按钮", Toast.LENGTH_SHORT).show();
}
break;
case R.id.button2:// 双击事件;
if (firstClick != 0) {
long secondClick = System.currentTimeMillis();
if (secondClick - firstClick < 500) {
Toast.makeText(this, "双击事件触发", Toast.LENGTH_SHORT).show();
}
}
firstClick = System.currentTimeMillis();
}
}
//
}