根据谷歌源码编辑多次点击事件

编写布局文件;

<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();

        }

    }

    //

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值