我已经onLongClickListener
在 AndroidDialog
中实现了使用计时器自动减少数字的按钮。这是代码
myIncreaseTimer = new Timer();
final Runnable runnable_decrease = new Runnable() {
public void run() {
int txt = Integer.parseInt(length.getText().toString());
txt -= 1;
length.setText(String.valueOf(txt));
if (txt == 0) {
myIncreaseTimer.cancel();
myIncreaseTimer.purge();
myIncreaseTimer = new Timer();
}
}
};
btnMinus.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
myIncreaseTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(runnable_decrease);
}
}, 0, 300);
return true;
}
});
当我在活动而不是对话框中时,我可以通过onTouchEvent
用户触摸屏幕来停止减少
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
myIncreaseTimer.cancel();
myIncreaseTimer.purge();
myIncreaseTimer = new Timer();
}
return true;
}
但是,我不能在对话框中执行此操作。更重要的是,对话框没有 onTouchEvent。
有谁知道我该如何实施或应该onTouchEvent
在Dialog
哪里停止增加计时器Dialog
?