Android 5秒后关闭对话框?

我正在开发可访问性应用程序。当用户想要离开该应用程序时,我会显示一个对话框,在该对话框中他必须确认要离开,如果5秒钟后他仍未确认,则该对话框应自动关闭(因为用户可能意外打开了该对话框)。这类似于在Windows上更改屏幕分辨率时发生的情况(会出现警报,如果您没有确认,它将恢复为以前的配置)。

这是我显示对话框的方式:

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

显示对话框后5秒钟如何关闭对话框?

回答:

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

以上是 Android 5秒后关闭对话框? 的全部内容, 来源链接: utcz.com/qa/407800.html

回到顶部