对话框中的 onTouchEvent
我已经在 Android Dialog
中实现了按钮的 onLongClickListener
,它使用计时器自动减少数字。这是代码
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。
有谁知道我如何在Dialog
中实现onTouchEvent
或者我应该在Dialog
中停止增加计时器?
I have implemented button's onLongClickListener
in Android Dialog
which auto-decreases number using timers. Here is the code
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;
}
});
When I am inside of activity, not dialog, I can stop the decrease via onTouchEvent
when a user touches the screen
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
myIncreaseTimer.cancel();
myIncreaseTimer.purge();
myIncreaseTimer = new Timer();
}
return true;
}
However, I cannot do this in the dialog. Even more, the dialog does not have onTouchEvent.
Does anyone know how can I either implement onTouchEvent
in Dialog
or where should I stop the increase timer in the Dialog
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法处理 Dialog 上的 onTouch,因此
创建一个 Activity,在其上实现 onTouchListener,
在清单文件中
所以,现在你的活动看起来像一个对话框,
You can't handle onTouch on Dialog so
Create a Activity, implement onTouchListener on it,
and in manifest file
So, now your activity is look like a DialogBox,
因为 Dialog 并非设计用于处理原始 onTouchEvents。考虑创建一个看起来像对话框的活动(透明主题,没有标题栏)并覆盖它的 onTouchEvent 方法。
because Dialog is not designed to handle raw onTouchEvents. consider creating an activity that would look like a dialog (transparent theme, no title bar) and override it's onTouchEvent method.