Android 如何为 UI 事件编写一个简单的计时器?
我的应用程序需要检查用户是否双击。但我不能使用 Android OnDoubleClickListener 或类似的东西。只是实现它:
我的问题与“正常”双击不同。
我想要的是: 如果用户双击,则运行 Y 活动。 如果用户仅单击 1 次,请等待 500 毫秒,然后运行 X 活动。 如果用户单击 2 缓慢单击,则运行 X 活动
这是我的代码:
long now = System.currentTimeMillis();
thisTime = now;
if (thisTime - lastTouchTime < WAIT_TIME) {
// Double tap
this.getContext()
.getApplicationContext()
.startActivity(
(new Intent(getContext(),
ChangePlaceActivity.class))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
// If is double tap, reset to start state.
lastTouchTime = -1;
return true;
} else {
// Too slow or first click
// If not double tap, save last state for next check
lastTouchTime = thisTime;
Log.d("Worker thread", "Declare" + System.currentTimeMillis());
Thread t = new Thread() {
public void run()
{
try {
sleep(WAIT_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Worker thread", "" + System.currentTimeMillis());
// start MainAct
Log.d("Single Click", "Yes");
this.getContext()
.getApplicationContext()
.startActivity(
(new Intent(getContext(),
MainActivity.class))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
如果使用这样的代码,我将无法双击。如果删除 thread.run() thread.join()。它将在 ChangePlacecActivity 之前启动 MainActivity。
对于这个问题有什么解决办法吗? 先感谢您!
抱歉我的英语不好。
My app needs to check whether user double clicks or not. But I can't use Android OnDoubleClickListener or something likes that. Just implements it :
My problem is diferent from "normal" double click.
What I want is:
If user double clicks, run the Y activity.
If user clicks 1 click only, wait 500 ms then run the X activity.
If user click 2 click slowly, run the X activity
This is my code:
long now = System.currentTimeMillis();
thisTime = now;
if (thisTime - lastTouchTime < WAIT_TIME) {
// Double tap
this.getContext()
.getApplicationContext()
.startActivity(
(new Intent(getContext(),
ChangePlaceActivity.class))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
// If is double tap, reset to start state.
lastTouchTime = -1;
return true;
} else {
// Too slow or first click
// If not double tap, save last state for next check
lastTouchTime = thisTime;
Log.d("Worker thread", "Declare" + System.currentTimeMillis());
Thread t = new Thread() {
public void run()
{
try {
sleep(WAIT_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Worker thread", "" + System.currentTimeMillis());
// start MainAct
Log.d("Single Click", "Yes");
this.getContext()
.getApplicationContext()
.startActivity(
(new Intent(getContext(),
MainActivity.class))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
If use code like this, I can't got double click. If remove thread.run() thread.join(). It will start MainActivity before ChangePlacecActivity.
Is there any solution for this problems?
Thank you in advance!
Sorry for my bad English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此使用 Handler 。像这样:(
类级声明)
(然后在onClick中)
请在盲目使用它之前自己调试一下,但我认为这是对的。
You can use a Handler for this. Something like:
(class-level declarations)
(and then in onClick)
Please debug this yourself before using it blindly, but I think it's right.