生成将自动解除的对话框

时间:2010-11-30 07:39:58

标签: android dialog

是否有可能创建一个Dialog,如果没有任何用户互动,会在一段时间后自动被解除?

5 个答案:

答案 0 :(得分:5)

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

在班级成员中:

private final int CANCEL_DIALOG = 1;
private Handler mHandler;
private Dialog mDialog;

在onCreate()中:

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);

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

答案 1 :(得分:2)

我最后使用Handler得到了它。

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);

Dialog内,我有一个ListView。在我scrollListener ListView的{​​{1}}中:

mHandler.removeMessages(CANCEL_DIALOG);
mHandler.sendEmptyMessageDelayed(CANCEL_DIALOG, 5000);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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);

答案 4 :(得分:-1)

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);

这里我们创建Timer对象并将计时器调度为2秒,超过2秒,然后自动调用run(),在run()中我们将编写逻辑来解除dislog

相关问题