Android开发之实现多次点击事件
在Android中给我们提供了单次点击事件。但并没有给我们提供双击,或者实现在一定时间内的多次事件。所以需要我们自己在单机监听上进行修改实现。
有如下两种实现方式:
1、定义一个存贮上一个第一次点击的变量,如果两次时间间隔小于500毫秒,则认为是双击时间。
实现如下:
- package com.andy.doubleclick;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.view.View;
- import android.widget.Toast;
- /**
- * @author Zhang,Tianyou
- * @version 2014年12月02日 上午10:51:56
- */
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- private long startClickTime;
- public void click(View view) {
- long nextClickTime = SystemClock.uptimeMillis();
- if (startClickTime <= 0) {
- startClickTime = SystemClock.uptimeMillis();
- return ;
- }else {
- if (nextClickTime - startClickTime < 500) {
- Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
- startClickTime = 0L;
- } else {
- startClickTime = SystemClock.uptimeMillis();
- }
- }
- }
- }
这种方式有个缺陷,如果要实现多次点击,那么就需要定义存贮多个事件点的变量,很显然不适合多次点击的处理。
2、使用Google提供的api中采用的算法。
能够实现n次点击事件,我们需要定义一个n长度的数组,每点击一次将数组里的内容按序号整体向左移动一格,然后给n-1出即数组的最后添加当前的时间,如果0个位置的时间大于当前时间减去500毫秒的话,那么证明在500毫秒内点击了n次。
实现如下:
- package com.andy.doubleclick;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.view.View;
- import android.widget.Toast;
- /**
- * @author Zhang,Tianyou
- * @version 2014年12月02日 上午10:51:56
- */
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- long[] mHits = new long[2];
- public void click(View view){
- //每点击一次 实现左移一格数据
- System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
- //给数组的最后赋当前时钟值
- mHits[mHits.length - 1] = SystemClock.uptimeMillis();
- //当0出的值大于当前时间-500时 证明在500秒内点击了2次
- if(mHits[0] > SystemClock.uptimeMillis() - 500){
- Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
- }
- }
- }
这种能够实现n此事件的点击,只需将数组长度定义为n个长度。
System.currentTimeMillis() 和 SystemClock.uptimeMillis()的区别:
在Api上是这么说的:
System.currentTimeMillis()is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (seesetCurrentTimeMillis), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to theACTION_TIME_TICK,ACTION_TIME_CHANGEDandACTION_TIMEZONE_CHANGEDIntentbroadcasts to find out when the time changes.SystemClock.uptimeMillisis counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such asThread.sleep(millls),Object.wait(millis), andSystem.nanoTime(). This clock is guaranteed to be monotonic, and is suitable for interval timing when the interval does not span device sleep. Most methods that accept a timestamp value currently expect theuptimeMillisclock.SystemClock.elapsedRealtimeandelapsedRealtimeNanosreturn the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.
SystemClock.elapsedRealtime : 从开机到现在的毫秒书(手机睡眠(sleep)的时间也包括在内)
System.currentTimeMillis() :从1970年1月1日 UTC到现在的毫秒数,是可以通过System.setCurrentTimeMillis修改的,那么,在某些情况下,一但被修改,时间间隔就不准了。
SystemClock.uptimeMillis : 它表示的是手机从启动到现在的运行时间,且不包括系统sleep(CPU关闭)的时间,很多系统的内部时间都是基于此,比如Thread.sleep(millls), Object.wait(millis), and System.nanoTime()它表示的是手机从启动到现在的运行时间,且不包括系统sleep(CPU关闭)的时间,很多系统的内部时间都是基于此,比如Thread.sleep(millls), Object.wait(millis), and System.nanoTime()
转自:https://blog.csdn.net/fengshizty/article/details/41675923
Android开发之实现多次点击事件的更多相关文章
- Android开发-之监听button点击事件
一.实现button点击事件的方法 实现button点击事件的监听方法有很多种,这里总结了常用的四种方法: 1.匿名内部类 2.外部类(独立类) 3.实现OnClickListener接口 4.添加X ...
- Android开发之去掉listview的点击效果,一行代码间接粗暴,解决你的问题。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发之去掉listview的点击效果,一行代码间接粗暴,解决你的问题. 当你在用list ...
- Android实现监听控件点击事件
Android实现监听控件点击事件 引言 这篇文章主要想写一下Android实现监听点击事件的几种方法,Activity和Fragment实现起来有些方法上会有些不同,这里也略做介绍. 最近一直在忙一 ...
- Android开发——Accessibility机制实现模拟点击(微信自动抢红包实现)
1. 何为Accessibility机制 许多Android使用者因为各种情况导致他们要以不同的方式与手机交互.对于那些由于视力.听力或其它身体原因导致不能方便使用Android智能手机的用户,And ...
- Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制
Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...
- Android开发之PullToRefresh的Click点击事件的监听实现长按删除Item
本文为原创博客.出自http://blog.csdn.net/minimicall 到今天为止,搜芽的卖家版本号应该来说已经基本完毕.攻坚克难的一路过来.速度也控制的比較好. 项目过程进度 从任务分配 ...
- Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件
转载:http://blog.csdn.net/aminfo/article/details/7872681 经常会在项目中用到透明图片,不规则图片,特别是做游戏的时候,需要对图片的透明区域的点击事件 ...
- Android TextView 高亮字体并添加点击事件
运行效果 package com.zutil.lib; import android.graphics.Typeface; import android.os.Bundle; import andro ...
- android 组合控件接收不到点击事件的问题
android点击事件的传播是有子控件传给父控件,如果子控件处理过了,父控件不再处理,所以要想让组合控件接收点击事件,必须屏蔽子控件的点击事件. 设置组合控件的clickable和focusable属 ...
随机推荐
- aspectj 表达式 execution切点函数
execution函数用于匹配方法执行的连接点,语法为: execution(方法修饰符(可选) 返回类型 方法名(参数) 异常模式(可选)) 参数部分允许使用通配符: * 匹配任意字符,但只能匹配 ...
- 20155205 2016-2017-2 《Java程序设计》第5周学习总结
20155205 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 如果没有try的话,出现异常会导致程序崩溃,而try则可以保证程序的正常运行下去.( ...
- 整数数字调节框QSpinBox
样式: import sys from PyQt5.QtWidgets import QApplication, QWidget, QSpinBox, QDoubleSpinBox, QHBoxLay ...
- proxysql 系列~审核功能
一 简介:今天我们来探讨下具体的审核功能 二 平台审计功能 一 proxysql 设置 set mysql-eventslog_filename = '/data/ProxySQL/log/sql. ...
- python 设计及调试的一些小技巧
在“笨办法学习python”中介绍了一些设计函数以及调试技巧: 参考网址:http://www.jb51.net/shouce/Pythonbbf/latest/ex36.html If 语句的规则¶ ...
- CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型
在昨天研究了发布CXF发布webservice之后想着将以前的项目发布webservice接口,可是怎么也发布不起来,服务启动失败,原来是自己的接口有返回值类型是Map. 研究了一番之后,发现: we ...
- javascript随笔和常见的知识点
1.js中循环中用 return只能停止循环,不能停止到函数的定义部分.所以下面的返回值为1 return 100没有意义,只起到终止循环的目的 function bb() { var sum = 0 ...
- 安装python3后使用pip和pip3的区别是什么?
安装python3后使用pip和pip3的区别是什么? 1.其实这两个命令效果是一样的,没有区别: (1)比如安装库numpy,pip3 install numpy或者pip install ...
- ES系列十二、ES的scroll Api及分页实例
1.官方api 1.Scroll概念 Version:6.1 英文原文地址:Scroll 当一个搜索请求返回单页结果时,可以使用 scroll API 检索体积大量(甚至全部)结果,这和在传统数据库中 ...
- 使用NGINX+Openresty和unixhot_waf开源防火墙实现WAF功能
使用NGINX+Openresty实现WAF功能 一.了解WAF1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: ...