當用戶抓住這個按鈕時改變顏色,並且當用戶離開按鈕,該按鈕返回到它的正常狀態。
是否有一個事件可以捕獲?
我試圖尋找類似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);
}
}
}
但是,這隻有當用戶將他的手指在按鈕上方舉起,當他將其拖到屏幕上的其他位置時,計數器保持運行,任何想法?
使用選擇更改它的狀態。看到鏈接http://android-journey.blogspot.in/2009/12/android-selectors.html –
你到底在做什麼?一個'OnTouchListener'可以用來「看到」什麼時候發生這種變化。 – Luksprog
添加額外的解釋,我試圖完成 –