2011-07-16 225 views
3

我懷疑是在一個特定的按鈕,或圖像視圖或視圖上實現的tap監聽器?因爲我衝浪的網站只顯示整個佈局,我希望我的動作能夠在視圖的敲擊下進行。請幫忙。謝謝。OnTap監聽器實現

回答

2

主要爲了感應輕拍事件,您必須在您的活動中實施GestureDetector.OnGestureListener。您可以使用onSingleTapUp(MotionEvent e)方法GestureDetector.OnGestureListener執行操作。

通常對於Button控件,我們使用像

myButton.setOnClickListener(new OnClickListener() {});

或其他clickListners和點擊事件被用於ImageView的或任何其他customviews。

9

任何視圖都可以使用作爲視圖類的一部分的onClickListener()進行設置。最簡單的方法是在onCreate()方法中設置對視圖的引用。下面是一個圖像視圖的示例:

ImageView iv = (ImageView) findViewByID(R.id.example); 
iv.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // Do what you need to do on click 
     .... 
    } 
}); 

UPDATE:DOUBLE TAP

下面是其上的圖像視圖實現了基本的雙敲擊檢測樣品活性:

import android.app.Activity; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class DoubleTapActivity extends Activity { 
    //Set the double tap delay in milliseconds 
    protected static final long DOUBLE_CLICK_MAX_DELAY = 1000L; 
    private ImageView iView; 
    private long thisTime = 0; 
    private long prevTime = 0; 
    private boolean firstTap = true; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     iView = (ImageView)findViewById(R.id.iView); 
     iView.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if(firstTap){ 
        thisTime = SystemClock.uptimeMillis(); 
        firstTap = false; 
       } 
       else 
       { 
        prevTime = thisTime; 
        thisTime = SystemClock.uptimeMillis(); 

        //Check that thisTime is greater than prevTime 
        //just incase system clock reset to zero 
        if(thisTime > prevTime){ 

         //Check if times are within our max delay 
         if((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY){ 

          //We have detected a double tap! 
          Toast.makeText(DoubleTapActivity.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show(); 
          //PUT YOUR LOGIC HERE!!!! 

         } 
         else 
         { 
          //Otherwise Reset firstTap 
          firstTap = true; 
         } 
        } 
        else 
        { 
         firstTap = true; 
        } 
       } 
       return false; 
      } 
     }); 
    } 
} 
+0

是的我知道這個代碼肯尼,但我要求可以雙擊自動收報機在一個特定的視圖或整個屏幕上實現。我正在製作一個項目,在這個項目中,我必須在不是整個屏幕的情況下只對視圖進行操作。 –

+1

你從未在你的問題的任何地方提及過雙擊!但我已經擴展了我的答案,向您展示了一種可能的方式。 – Kenny

0

@Override public boolean onTouchEvent(MotionEvent event){

// on action down do your work... 
    return super.onTouchEvent(event); 
} 
1

在您想要點擊的佈局中添加android:onClick="tapEvent"。通過更改MAX_TAP_COUNT值,您可以使用任意數量的水龍頭。

private long thisTime = 0; 
private long prevTime = 0; 
private int tapCount = 0; 
private static final int MAX_TAP_COUNT = 5; 
protected static final long DOUBLE_CLICK_MAX_DELAY = 500; 


public void tapEvent(View v){ 

    if (SystemClock.uptimeMillis() > thisTime) { 
     if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) { 
      Log.d(TAG, "touch event " + "resetting tapCount = 0"); 
      tapCount = 0; 
     } 
     if (tapCount()) { 
      //DO YOUR LOGIC HERE 
     } 
    } 
} 

private Boolean tapCount(){ 

    if (tapCount == 0) { 
     thisTime = SystemClock.uptimeMillis(); 
     tapCount++; 
    } else if (tapCount < (MAX_TAP_COUNT-1)) { 
     tapCount++; 
    } else { 
     prevTime = thisTime; 
     thisTime = SystemClock.uptimeMillis(); 

     //just incase system clock reset to zero 
     if (thisTime > prevTime) { 

      //Check if times are within our max delay 
      if ((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) { 
       //We have detected a multiple continuous tap! 
       //Once receive multiple tap, reset tap count to zero for consider next tap as new start 
       tapCount = 0; 
       return true; 
      } else { 
       //Otherwise Reset tapCount 
       tapCount = 0; 
      } 
     } else { 
      tapCount = 0; 
     } 
    } 
    return false; 

}