x秒后启用alertdialog"ok"按钮

Mdl*_*dlc 2 android dialog

我有一个对话框,当应用程序第一次启动时显示信息.由于这些天用户,总是点击"确定"而不阅读文本.我想在前5秒内禁用OK按钮(最好在里面倒计时).如何实现这一目标?

我的代码(不是很必要):

  new AlertDialog.Builder(this)
        .setMessage("Very usefull info here!")
        .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
             ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
             // the rest of your stuff
        }
        })
        .show();
Run Code Online (Sandbox Code Playgroud)

我希望这对其他用户有帮助.

Ken*_*olf 10

干得好:

// Create a handler
Handler handler = new Handler();

// Build the dialog
AlertDialog dialog = new AlertDialog.Builder(this)
    .setMessage("Very usefull info here!")
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the rest of your stuff
    }
})
.create();

dialog.show();

// Access the button and set it to invisible
final Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setVisibility(View.INVISIBLE);

// Post the task to set it visible in 5000ms         
handler.postDelayed(new Runnable(){
    @Override
    public void run() {
        button.setVisibility(View.VISIBLE); 
    }}, 5000);
Run Code Online (Sandbox Code Playgroud)

这将在5秒后启用按钮.这看起来有点乱,但它确实有效.我欢迎任何拥有更清洁版本的人!