Android多击事件就是这么简单

Android多击事件就是这么简单

android代码

转载请注明https://blog.csdn.net/wangzhenhuait/article/details/109047070

下面是具体代码,代码参考安卓源码实现的多击事件 代码片.

package com.example.doubleclick;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Intent;
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 long startTime = 0;
	private long[] mHits = new long[5];//几击事件,数组就是几,双击事件就是2
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button bt_click = (Button) findViewById(R.id.bt_click);
		Button bt_many_click = (Button) findViewById(R.id.bt_many_click);
		
		bt_click.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//两次点击在一定的时间间隔内,才是双击
				if(startTime!=0){
					long endTime = System.currentTimeMillis();
					if(endTime-startTime<500){
						Toast.makeText(getApplicationContext(), "是男人", 0).show();
					}
				}
				startTime = System.currentTimeMillis();
			}
		});
		
		bt_many_click.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//了解
				//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){
		        	//响应了一个三击事件
		        	Toast.makeText(getApplicationContext(), "超级赛亚人!!!!", 0).show();
		        }
			}
		});
		
		
	/*	System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
        mHits[mHits.length-1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClassName("android",
                    com.android.internal.app.PlatLogoActivity.class.getName());
            try {
                startActivity(intent);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
            }
        }*/
	}

}

xml的配置文件

<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bt_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/bt_many_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="many_click" />

</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值