Android开发之实现多次点击事件
2014-12-02 17:38
411 查看
在Android中给我们提供了单次点击事件。但并没有给我们提供双击,或者实现在一定时间内的多次事件。所以需要我们自己在单机监听上进行修改实现。
有如下两种实现方式:
1、定义一个存贮上一个第一次点击的变量,如果两次时间间隔小于500毫秒,则认为是双击时间。
实现如下:
这种方式有个缺陷,如果要实现多次点击,那么就需要定义存贮多个事件点的变量,很显然不适合多次点击的处理。
2、使用Google提供的api中采用的算法。
能够实现n次点击事件,我们需要定义一个n长度的数组,每点击一次将数组里的内容按序号整体向左移动一格,然后给n-1出即数组的最后添加当前的时间,如果0个位置的时间大于当前时间减去500毫秒的话,那么证明在500毫秒内点击了n次。
实现如下:
这种能够实现n此事件的点击,只需将数组长度定义为n个长度。
System.currentTimeMillis() 和 SystemClock.uptimeMillis()的区别:
在Api上是这么说的:
application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the
by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as
for general purpose interval timing.
and
and
有如下两种实现方式:
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 (see
setCurrentTimeMillis), 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 the
ACTION_TIME_TICK,
ACTION_TIME_CHANGEDand
ACTION_TIMEZONE_CHANGED
Intentbroadcasts 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 as
Thread.sleep(millls),
Object.wait(millis), and
System.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 the
uptimeMillisclock.
SystemClock.elapsedRealtimeand
elapsedRealtimeNanosreturn 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()
相关文章推荐
- Android开发之源码:多次点击事件的原理和实现
- Android开发之实现多次点击事件
- [置顶] Android开发之RecyclerView实现点击事件和长按事件
- Android开发-三种方法实现点击事件
- Android实现多次点击监听事件!(4次为例)
- android中多次点击事件的实现
- Android开发之PullToRefresh的Click点击事件的监听实现长按删除Item
- Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码
- android的多次点击事件的实现(有源码)
- Android开发——利用ViewPager实现显示多个item无限轮播+点击事件监听
- Android开发:ListView系列之item点击事件的实现及点击事件失效的解决办法
- Android移动开发-检测点击按键事件的实现
- Android 连续多次点击事件的实现
- 防止Android点击按钮过快造成多次事件实现原理
- 快速开发11之多次点击事件实现功能的万能代码
- Android开发中在ListView中经常有Item的点击事件及Button或ImageButton等需要被点击的事件之间的处理方法
- 实现在android开发中重写返回键处理事件
- 关于Android游戏开发中OnTouchEvent()触屏事件的性能优化!---------------用锁synchronized来实现!!
- Android开发(11)-利用listview控件显示person表中的所有数据(并实现点击触发拨号程序)
- Android开发--为按钮添加点击事件