【问题标题】:Automatic dialog dismiss in AndroidAndroid中的自动对话框关闭
【发布时间】:2012-11-14 03:40:06
【问题描述】:

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

【问题讨论】:

  • 写一个线程可能会在显示对话后运行两分钟...如果用户没有做出任何响应,请使用dismiss()关闭对话框...在关闭之前我认为它更好检查对话框对象是否为空。如果它为空,我认为你不需要解雇,如果不解雇它

标签: android view dialog dismiss


【解决方案1】:
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)。

【讨论】:

    【解决方案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);
    

    【讨论】:

    • 尝试在您的代码中添加一些描述。你的代码是做什么的?
    【解决方案3】:

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

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

    Android timer? How-to?

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

    【讨论】:

      【解决方案4】:
      final Timer t = new Timer();
                      t.schedule(new TimerTask() {
                          public void run() {
                              alert.dismiss(); 
                              t.cancel(); 
                          }
                      }, 2000);
      

      【讨论】:

        猜你喜欢
        • 2013-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多