3

我正在创建一个 Android 应用程序,我在其中管理一些剩余部分。我希望当某个事件发生时显示对话框,这对我来说不是问题。但我希望如果用户在两分钟内没有做出任何响应,对话框会自动关闭。我该如何实施?

4

4 回答 4

5
static AlertDialog alert = ....;


alert.show();

Runnable dismissRunner = new Runnable() {
    public void run() {
        if( alert != null )
            alert.dismiss();            
    };
new Handler().postDelayed( dismissRunner, 120000 );

不要忘记alert = null在您的常规对话框中关闭代码(即按钮 onClick)。

于 2012-11-26T16:16:55.860 回答
2
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.game_message);
        game_message = builder.create();
        game_message.show();


        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                game_message.dismiss(); // when the task active then close the dialog
                t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
            }
        }, 5000);
于 2019-03-10T09:01:22.643 回答
1

您应该能够使用 Timer 做到这一点:

http://developer.android.com/reference/java/util/Timer.html

安卓计时器?如何?

stackoverflow 链接描述了如何使用它来运行重复发生的任务,但您也可以使用它来运行一次性任务。

于 2012-11-26T16:14:28.303 回答
1
final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        alert.dismiss(); 
                        t.cancel(); 
                    }
                }, 2000);
于 2015-07-01T06:41:41.073 回答