Android 如何为 UI 事件编写一个简单的计时器?

发布于 2024-12-26 01:14:27 字数 2224 浏览 3 评论 0原文

我的应用程序需要检查用户是否双击。但我不能使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-01-02 01:14:28

您可以为此使用 Handler 。像这样:(

类级声明)

Handler handler = new Handler();
Runnable singleClickTask = new Runnable() {
  public void run() {
    //run X activity
    firstClick = true;
  }
};
boolean firstClick = true;

(然后在onClick中)

if (firstClick) {
  handler.postDelayed(singleClickTask, 500);
  firstClick=false;
} else {
  handler.removeCallbacks(singleClickTask);
  firstClick=true;
  //run Y activity
}

请在盲目使用它之前自己调试一下,但我认为这是对的。

You can use a Handler for this. Something like:

(class-level declarations)

Handler handler = new Handler();
Runnable singleClickTask = new Runnable() {
  public void run() {
    //run X activity
    firstClick = true;
  }
};
boolean firstClick = true;

(and then in onClick)

if (firstClick) {
  handler.postDelayed(singleClickTask, 500);
  firstClick=false;
} else {
  handler.removeCallbacks(singleClickTask);
  firstClick=true;
  //run Y activity
}

Please debug this yourself before using it blindly, but I think it's right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文