Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之實現多次點擊事件

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

編輯:關於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 (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_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()




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved