码迷,mamicode.com
首页 > 其他好文 > 详细

兔子--多击事件

时间:2014-12-10 21:21:31      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:多击事件

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;
	}

}

兔子--多击事件

标签:多击事件

原文地址:http://blog.csdn.net/u013425527/article/details/41849507

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!