2012-09-11 77 views
2

可能重複:
onPress/onRelease in Android事件按鈕

當用戶抓住這個按鈕時改變顏色,並且當用戶離開按鈕,該按鈕返回到它的正常狀態。

是否有一個事件可以捕獲?

我試圖尋找類似button.onStateChanged,但沒有運氣..

編輯: 當用戶點擊長我要開始自動增量

// Auto increment for a long click 
     increment.setOnLongClickListener(new View.OnLongClickListener() { 
      public boolean onLongClick(View a) { 
       autoIncrement = true; 

       repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler); 
       repeatUpdateHandler.post(mRepeatUpdateHandler); 

       return false; 
      } 
     }); 

,並在用戶離開用手指按鈕,我想停止自動增量

increment.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { 

       //Stop autoincrement 

       repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler); 
       autoIncrement = false; 
      } 

      return false; 
     } 
    }); 

這是處理程序:

RepetetiveUpdater mRepeatUpdateHandler = new RepetetiveUpdater(); 
.... 

class RepetetiveUpdater implements Runnable { 
    public void run() { 
     if (autoIncrement) { 
      increment(); 
      repeatUpdateHandler.postDelayed(new RepetetiveUpdater(), 
        REPEAT_DELAY); 
     } else if (autoDecrement) { 
      decrement(); 
      repeatUpdateHandler.postDelayed(new RepetetiveUpdater(), 
        REPEAT_DELAY); 
     } 
    } 
} 

但是,這隻有當用戶將他的手指在按鈕上方舉起,當他將其拖到屏幕上的其他位置時,計數器保持運行,任何想法?

+0

使用選擇更改它的狀態。看到鏈接http://android-journey.blogspot.in/2009/12/android-selectors.html –

+0

你到底在做什麼?一個'OnTouchListener'可以用來「看到」什麼時候發生這種變化。 – Luksprog

+0

添加額外的解釋,我試圖完成 –

回答

4

我不知道如果我下面的代碼做你想要做什麼。遞增方法將被調用,直到用戶停止點擊或他的手指不在視圖的邊界內。所有在onTouch回調做:

final Rect r = new Rect(); 
    // this is the platform's default timeout for recognizing a long press 
    final long LONG_PRESS_TIMOUT = ViewConfiguration.getLongPressTimeout(); 
    increment.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      final int action = event.getAction(); 
      // get the View's Rect relative to its parent 
      v.getHitRect(r); 
      // offset the touch coordinates with the values from r 
      // to obtain meaningful coordinates 
      final float x = event.getX() + r.left; 
      final float y = event.getY() + r.top; 
      switch (action) { 
      case MotionEvent.ACTION_DOWN: 
       // simulate a long press 
       // if the user stops pressing before the long press timeout 
       // expires this will be canceled 
       repeatUpdateHandler.postDelayed(mRepeatUpdateHandler, 
         LONG_PRESS_TIMOUT); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       // if the touch coordinates are not in the Views' rect then 
       // cancel any Runnable 
       if (!r.contains((int) x, (int) y)) { 
        repeatUpdateHandler 
          .removeCallbacks(mRepeatUpdateHandler); 
       } 
       break; 
      case MotionEvent.ACTION_UP: 
       // the user raised his finger, cancel the Runnable 
       repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler); 
       break; 
      } 
      return false; 
     } 
    }); 

而且Runnable部分:

class RepetetiveUpdater implements Runnable { 
    public void run() { 
     increment(); 
     repeatUpdateHandler.postDelayed(mRepeatUpdateHandler, 1000); 
    } 
} 

int i = 0; 

public void increment() { 
    Log.e("INCREMENT", "Value " + (++i)); 
} 
+0

Thx,我可以用這個:) –

0

您可以用不同的方式使用onTouch()或onFocusChange()偵聽器來獲取按鈕。我不確定您只是嘗試一次。

E.g:

 button.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    button.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return false; 
     } 
    }); 
0
@Override 
public boolean onTouch(View view, MotionEvent motionEvent) { 
    // TODO Auto-generated method stub 

    textX.setText("x: " + String.valueOf(motionEvent.getX())); 
    textY.setText("y: " + String.valueOf(motionEvent.getY())); 

    int action = motionEvent.getAction(); 

    switch (action){ 
    case MotionEvent.ACTION_DOWN: 
    textEvent.setText("ACTION_DOWN");  
    break; 
    case MotionEvent.ACTION_MOVE: 
    textEvent.setText("ACTION_MOVE"); 
    break; 
    case MotionEvent.ACTION_UP: 
    textEvent.setText("ACTION_UP");   // you need this! 
    break; 
    case MotionEvent.ACTION_CANCEL: 
    textEvent.setText("ACTION_CANCEL"); 
    break; 
    default: 
    textEvent.setText("Unknown!"); 
    } 

    return true; //means event have been processed 
} 
+1

投下來,因爲它是一個純粹的純代碼答案。更像寫這個方法應該放在哪裏以及它如何工作。描述性並分享你的知識,而不是你的代碼。改變這一點,我會恢復或甚至可能投票。 – WarrenFaith