首页 > 代码库 > 兔子--多击事件

兔子--多击事件

package com.example.myclickmore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btn;
	private long[] mHits = new long[3];
	int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Intent intent = new Intent();
		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println(+mHits.length);
				i++;
				Log.i("i", i + "");
				btn.setText(i + "");

				// System.arraycopy实现的功能数组的内部元素自拷贝,
				// 每次mHits[0]都被mHits[1]覆盖
				// 从而判断是否是按连击事件处理。
				// 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.

				System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
				mHits[mHits.length - 1] = SystemClock.uptimeMillis(); // 系统开机时间
				btn.setText(i + SystemClock.uptimeMillis() + "");
				if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
					Toast.makeText(MainActivity.this, "这就是传说中的多击事件",
							Toast.LENGTH_LONG).show();

					intent.setClass(MainActivity.this, MainActivity1.class);
					startActivity(intent);

				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

兔子--多击事件