【问题标题】:set timeout Dialog in android?在android中设置超时对话框?
【发布时间】:2011-01-25 22:23:20
【问题描述】:

我想在android中设置Dialog(进度对话框)的超时时间,使对话框在一段时间后消失(如果某些操作没有响应!)

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    验证与this post 中的方法相同(使用 long 而不是 float):

    public void timerDelayRemoveDialog(long time, final Dialog d){
        new Handler().postDelayed(new Runnable() {
            public void run() {                
                d.dismiss();         
            }
        }, time); 
    }
    

    【讨论】:

    • 非常方便。谢谢老兄。
    【解决方案2】:

    您始终可以创建一个名为 ProgressDialogWithTimeout 的类,并覆盖 show 方法的功能以返回 ProgressDialog 并设置一个计时器,以便在该计时器关闭时执行您希望的操作。示例:

    private static Timer mTimer = new Timer();
    private static ProgressDialog dialog;
    
    public ProgressDialogWithTimeout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    public ProgressDialogWithTimeout(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
    }
    
    public static ProgressDialog show (Context context, CharSequence title, CharSequence message)
    {
        MyTask task = new MyTask();
                // Run task after 10 seconds
        mTimer.schedule(task, 0, 10000);
    
        dialog = ProgressDialog.show(context, title, message);
        return dialog;
    }
    
    static class MyTask extends TimerTask {
    
        public void run() {
            // Do what you wish here with the dialog
            if (dialog != null)
            {
                dialog.cancel();
            }
        }
    }
    

    然后你会在你的代码中这样调用它:

    ProgressDialog progressDialog = ProgressDialogWithTimeout.show(this, "", "Loading...");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多