Android開發之實現多次點擊事件

來源:互聯網
上載者:User

標籤:googl   state   second   void   數組   time_t   text   java   cte   

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

 

 

 

轉自:41675923

Android開發之實現多次點擊事件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.