• Android开发之实现多次点击事件


    在Android中给我们提供了单次点击事件。但并没有给我们提供双击,或者实现在一定时间内的多次事件。所以需要我们自己在单机监听上进行修改实现。

    有如下两种实现方式:

    1、定义一个存贮上一个第一次点击的变量,如果两次时间间隔小于500毫秒,则认为是双击时间。

         实现如下:

    1.  
      package com.andy.doubleclick;
    2.  
       
    3.  
      import android.app.Activity;
    4.  
      import android.os.Bundle;
    5.  
      import android.os.SystemClock;
    6.  
      import android.view.View;
    7.  
      import android.widget.Toast;
    8.  
       
    9.  
      /**
    10.  
      * @author Zhang,Tianyou
    11.  
      * @version 2014年12月02日 上午10:51:56
    12.  
      */
    13.  
       
    14.  
      public class MainActivity extends Activity {
    15.  
       
    16.  
      @Override
    17.  
      protected void onCreate(Bundle savedInstanceState) {
    18.  
      super.onCreate(savedInstanceState);
    19.  
      setContentView(R.layout.activity_main);
    20.  
      }
    21.  
       
    22.  
      private long startClickTime;
    23.  
       
    24.  
      public void click(View view) {
    25.  
      long nextClickTime = SystemClock.uptimeMillis();
    26.  
      if (startClickTime <= 0) {
    27.  
      startClickTime = SystemClock.uptimeMillis();
    28.  
      return ;
    29.  
      }else {
    30.  
      if (nextClickTime - startClickTime < 500) {
    31.  
      Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
    32.  
      startClickTime = 0L;
    33.  
      } else {
    34.  
      startClickTime = SystemClock.uptimeMillis();
    35.  
      }
    36.  
       
    37.  
      }
    38.  
       
    39.  
      }
    40.  
       
    41.  
      }


    这种方式有个缺陷,如果要实现多次点击,那么就需要定义存贮多个事件点的变量,很显然不适合多次点击的处理。

    2、使用Google提供的api中采用的算法。

          能够实现n次点击事件,我们需要定义一个n长度的数组,每点击一次将数组里的内容按序号整体向左移动一格,然后给n-1出即数组的最后添加当前的时间,如果0个位置的时间大于当前时间减去500毫秒的话,那么证明在500毫秒内点击了n次。

         实现如下:

       

    1.  
      package com.andy.doubleclick;
    2.  
       
    3.  
      import android.app.Activity;
    4.  
      import android.os.Bundle;
    5.  
      import android.os.SystemClock;
    6.  
      import android.view.View;
    7.  
      import android.widget.Toast;
    8.  
       
    9.  
      /**
    10.  
      * @author Zhang,Tianyou
    11.  
      * @version 2014年12月02日 上午10:51:56
    12.  
      */
    13.  
       
    14.  
      public class MainActivity extends Activity {
    15.  
       
    16.  
      @Override
    17.  
      protected void onCreate(Bundle savedInstanceState) {
    18.  
      super.onCreate(savedInstanceState);
    19.  
      setContentView(R.layout.activity_main);
    20.  
      }
    21.  
       
    22.  
       
    23.  
      long[] mHits = new long[2];
    24.  
      public void click(View view){
    25.  
      //每点击一次 实现左移一格数据
    26.  
      System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
    27.  
      //给数组的最后赋当前时钟值
    28.  
      mHits[mHits.length - 1] = SystemClock.uptimeMillis();
    29.  
      //当0出的值大于当前时间-500时 证明在500秒内点击了2次
    30.  
      if(mHits[0] > SystemClock.uptimeMillis() - 500){
    31.  
      Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
    32.  
      }
    33.  
      }
    34.  
       
    35.  
      }


    这种能够实现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_TICKACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

    • SystemClock.uptimeMillis is 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 uptimeMillis clock.

    • SystemClock.elapsedRealtime and elapsedRealtimeNanos return 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

  • 相关阅读:
    Vue
    Vue
    Vue
    Vue
    Vue
    kubernetes
    kubernetes
    kubernetes
    django源码bug解决方案
    UNI-APP 桌面LOGO角标设置(ios)
  • 原文地址:https://www.cnblogs.com/tc310/p/9599205.html
Copyright © 2020-2023  润新知