gpt4 book ai didi

android - 如何在android中关闭一个AlertDialog

转载 作者:行者123 更新时间:2023-11-29 00:35:47 26 4
gpt4 key购买 nike

我正在开发一个示例 android 应用程序,它在警报对话框中显示数字(1 到 5),并且数字会在一秒钟后发生变化。现在我想在 5 秒后关闭 AlertDialog。我如何关闭警报对话框。您可以检查我到目前为止开发的代码。请在这方面帮助我。我将不胜感激帮助。

    timeView = new TextView(this);

AlertDialog.Builder builder = new AlertDialog.Builder(this);


builder.setView(timeView);
builder.setTitle("Time");
builder.setCancelable(false);
builder.show();




timeView.postDelayed(new Runnable()
{
public void run()

{

switch(timeSecond)
{

case 0:timeView.setText("5");
timeSecond++;
break;

case 1:timeView.setText("4");
timeSecond++;
break;

case 2:timeView.setText("3");
timeSecond++;
break;

case 3:timeView.setText("2");
timeSecond++;
break;

case 4:timeView.setText("1");
timeSecond++;
break;


}
if(timeSecond!=5)
{
timeView.postDelayed(this,1000);

}

else
{
//Here I want to close the Alert Dialog

}

}
}, 1000);

最佳答案

final AlertDialog dialog = builder.show();
....
dialog.dismiss()

将关闭对话框。

编辑:对于您上面的代码,我建议替代方案:

    if (!conditionTrue) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Hello");
builder.setMessage("Hello Android");
builder.setCancelable(false);
builder.show();
}

如果您想关闭它,则无需显示对话框。

关于android - 如何在android中关闭一个AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12785687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com