Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 生成将自动取消的对话框_Android_Dialog - Fatal编程技术网

Android 生成将自动取消的对话框

Android 生成将自动取消的对话框,android,dialog,Android,Dialog,是否可以创建一个对话框,如果没有任何用户交互,该对话框将在一段时间后自动关闭?您可以将一个活动用作对话框,然后在一段时间后(根据需要)完成它。您可以使用处理程序自动关闭它 在班级成员中: private final int CANCEL_DIALOG = 1; private Handler mHandler; private Dialog mDialog; 在onCreate()中: 在用于打开对话框(或使用的任何系统)的按钮上: 基本上在5秒钟后,对话框将在使用此代码打开后关闭。我最终使用

是否可以创建一个
对话框,如果没有任何用户交互,该对话框将在一段时间后自动关闭?

您可以将一个活动用作对话框,然后在一段时间后(根据需要)完成它。

您可以使用处理程序自动关闭它

在班级成员中:

private final int CANCEL_DIALOG = 1;
private Handler mHandler;
private Dialog mDialog;
在onCreate()中:

在用于打开对话框(或使用的任何系统)的按钮上:


基本上在5秒钟后,对话框将在使用此代码打开后关闭。

我最终使用
处理程序获得它

mHandler = new Handler(new Handler.Callback()
{
    @Override
    public boolean handleMessage(Message msg)
    {
        if(msg.what == CANCEL_DIALOG)
        {
            mDialog.cancel();
        }

        return false;
    }
});

mDialog.show();
mHandler.sendEmptyMessageDelayed(CANCEL_DIALOG, 5000);
对话框中
我有一个
列表视图
。在
ListView
scrollListener
中,我放了:

mHandler.removeMessages(CANCEL_DIALOG);
mHandler.sendEmptyMessageDelayed(CANCEL_DIALOG, 5000);
在这里,我们创建Timer对象,并将计时器安排为2秒。一旦超过2秒,它就会自动调用run(),在run()中,我们将编写逻辑来消除您可以使用的dislog

就这么简单

 new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                 //Your AlertDialog.cancel
                dialog.cancel();
            }
        }.start();

我在handleMessage()中放了一些日志。该函数不可执行如果它工作,则对话框将在5秒后完全关闭,无论对话框是否得到交互。如果对话框正在进行交互,则不应将其取消……是否可能……它应该工作,日志是在If语句中还是在handleMessage()的开头?是否在Activities onCreate()方法中创建了处理程序(已重写)?如果您想使对话框接收到交互时计时器停止,那么可以调用mHandler.removeMessages(CANCEL_Dialog);好的,谢谢。。它对我很有效…现在我需要检查第二个是否有效…谢谢…我在对话框中有一个列表视图。那么我应该在OnScrollListener中写些什么呢??请回复一些解释可能会有帮助。在同一帖子中复制粘贴另一个答案似乎不公平。
mHandler.removeMessages(CANCEL_DIALOG);
mHandler.sendEmptyMessageDelayed(CANCEL_DIALOG, 5000);
dialog.show();
final Timer t = new Timer();
t.schedule(new TimerTask() 
{
    public void run() 
    {
    dialog.dismiss(); // when the task active then close the dialog
    t.cancel(); // also just top the timer thread,otherwise, you may receive a crash report
    }
}, 2000);
final Timer t = new Timer();

            t.schedule(new TimerTask() {

                public void run() {

                    dlg.dismiss(); // when the task active then close the 
                                    // dialog(here we are dismissing the dialog)

                    t.cancel(); // also just top the timer thread,
                                // otherwise, you may receive a crash report

                }

            }, 2000);
 new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                 //Your AlertDialog.cancel
                dialog.cancel();
            }
        }.start();