Button多击事件的实现



    
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];
    
    @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(), "5连击!!!!", 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());
            }
        }*/
    }

}

你可能感兴趣的:(Button多击事件的实现)