如何自动关闭自定义对话框

huangapple go评论74阅读模式
英文:

How to close a custom dialog automatically

问题

我想打开一个对话框。并在几秒钟后自动关闭,对话框中的按钮也应该在发生任何事件后关闭对话框,无论哪种方式先发生。但我找不到在时间到期后关闭对话框的正确方法。

我使用下面的自定义对话框:

private void okShowDialog(String title, String message){
    vibrate();
    final Dialog dialogo=new Dialog(Login.this);
    dialogo.setContentView(R.layout.okdialog);
    dialogo.setCancelable(false);
    TextView errorTitle=dialogo.findViewById(R.id.lblTitleDialog);
    errorTitle.setText(title);
    TextView errorMessage=dialogo.findViewById(R.id.txtErrorDialog);
    errorMessage.setText(message);
    Button dialogButton = (Button) dialogo.findViewById(R.id.btnCont);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    dialogo.show();
}

对话框的XML非常简单,只显示标题、消息和按钮。

我已经花了几天时间,但无法找出如何解决这个问题。

英文:

I want to open a dialog. And dismiss automatically after a few seconds, the button in the dialog should also dismiss a dialog, whatever happens first. But I can't find the right way to close the dialog after time is up

I use the next custom dialog

private void okShowDialog(String title, String message){
    vibrate();
    final Dialog dialogo=new Dialog(Login.this);
    dialogo.setContentView(R.layout.okdialog);
    dialogo.setCancelable(false);
    TextView errorTitle=dialogo.findViewById(R.id.lblTitleDialog);
    errorTitle.setText(title);
    TextView errorMessage=dialogo.findViewById(R.id.txtErrorDialog);
    errorMessage.setText(message);
    Button dialogButton = (Button) dialogo.findViewById(R.id.btnCont);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    dialogo.show();
}

The dialog XML is very simple, it just shows a title, message, and button.

I've been through this for a couple of days and can't figure out how to solve it.

答案1

得分: 5

你可以尝试添加 Handler:

dialogo.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        // 在1000毫秒后关闭对话框
        dialogo.cancel();
    }
}, 1000);

在1000毫秒(1秒)后,你的对话框将会关闭。我认为你不必检查对话框是否被按钮关闭,当你再次调用close方法关闭已经关闭的对话框时,你不会收到任何错误。但如果我错了,只需添加一个布尔变量来控制对话框是否被按钮关闭。

英文:

You can try to add Handler:

dialogo.show();

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        // Close dialog after 1000ms
        dialogo.cancel();
    }
}, 1000);

After 1000 ms (1 sec) Your dialog will be closed. I think that You don't have to check if a dialog was closed with a button and when You call again close on closed dialog You won't get any error but if I am not right just add a boolean variable to control if a dialog was closed by a button.

答案2

得分: 4

你也可以使用Kotlin协程:

'构建您的对话框...'
dialog.setOnShowListener {
    CoroutineScope(Dispatchers.Main).launch {
        delay(length)
        dialog.dismiss()
    }
}
dialog.show()

其中'length'是您想要对话框显示的毫秒数。

英文:

You could also use Kotlin Coroutine:

'build your dialog...'
dialog.setOnShowListener {
    CoroutineScope(Dispatchers.Main).launch {
        delay(length)
        dialog.dismiss()
    }
}
dialog.show()

Where 'length' is the time in milliseconds that you want the dialog to show for.

huangapple
  • 本文由 发表于 2020年7月23日 00:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63038587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定