5秒后的And​​r​​oid关闭对话框? [英] Android close dialog after 5 seconds?

查看:267
本文介绍了5秒后的And​​r​​oid关闭对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个accesibility应用程序。当用户想要离开我的应用程序显示一个对话框,在这里,他必须确认他要离开,如果他不5秒对话框后,确认应自动关闭(因为用户可能打开了意外)。这类似于在Windows上会发生什么,当您更改屏幕分辨率(出现警报,如果你不确认它,它恢复到previous配置)。

这是我怎么显示对话框:

  AlertDialog.Builder对话框=新AlertDialog.Builder(本).setTitle(离开发射器)setMessage(你确定要离开了发射器​​?)。
            dialog.setPositiveButton(确认,新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释whichButton){
                    exitLauncher();
                }
            });
            。dialog.create()显示();
 

我怎样才能显示它后,关闭对话框5秒?

解决方案

 最后AlertDialog.Builder对话框=新AlertDialog.Builder(本).setTitle(离开发射器)。setMessage( 你确定你要离开发射器?);
dialog.setPositiveButton(确认,新DialogInterface.OnClickListener(){
    @覆盖
    公共无效的onClick(DialogInterface对话,诠释whichButton){
        exitLauncher();
    }
});
最后AlertDialog警报= dialog.create();
alert.show();

//在几秒钟后隐藏
最终的处理程序处理程序=新的处理程序();
最终的可运行可运行=新的Runnable(){
    @覆盖
    公共无效的run(){
        如果(alert.isShowing()){
            alert.dismiss();
        }
    }
};

alert.setOnDismissListener(新DialogInterface.OnDismissListener(){
    @覆盖
    公共无效onDismiss(DialogInterface对话){
        handler.removeCallbacks(可运行);
    }
});

handler.postDelayed(可运行,10000);
 

I'm working on an accesibility app. When the user wants to leave the app I show a dialog where he has to confirm he wants to leave, if he doesn't confirm after 5 seconds the dialog should close automatically (since the user probably opened it accidentally). This is similar to what happens on Windows when you change the screen resolution (an alert appears and if you don't confirm it, it reverts to the previous configuration).

This is how I show the dialog:

AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
            dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    exitLauncher();
                }
            });
            dialog.create().show();

How can I close the dialog 5 seconds after showing it?

解决方案

final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int whichButton) {
        exitLauncher();
    }
});     
final AlertDialog alert = dialog.create();
alert.show();

// Hide after some seconds
final Handler handler  = new Handler();
final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (alert.isShowing()) {
            alert.dismiss();
        }
    }
};

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(runnable);
    }
});

handler.postDelayed(runnable, 10000);

这篇关于5秒后的And​​r​​oid关闭对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆