gpt4 book ai didi

Android 在设置时间后隐藏对话框,就像自定义时间间隔 toast

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:15 24 4
gpt4 key购买 nike

我正在尝试在屏幕上显示一些文本一段时间,类似于 toast ,但能够指定它在屏幕上的确切时间。我认为警告对话框可能适用于此,但我似乎无法弄清楚如何自动关闭该对话框。

您能否建议一种 toast 通知的替代方案,我可以在其中指定显示的确切时间?

谢谢!

static DialogInterface dialog = null;
public void toast(String text, int duration) {

final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
((TextView)layout.findViewById(R.id.message)).setText(text);

builder
.setView(layout);


builder.show();

if (dialog!=null){
dialog.cancel();
dialog.dismiss();
}

dialog = builder.create();


Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
dialog.cancel();
dialog.dismiss();
}
}, 500);


}

最佳答案

您只需要计时调用Dialog#dismiss();对于您的问题 Handler 类就足够了(+1 到 Javanator :))。

仅供引用,还有其他类即 AlarmManager , Timer & TimerTask这有助于计时代码的运行。

编辑:

将您的代码更改为:

static DialogInterface dialog = null;
public void toast(String text, int duration) {

final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
((TextView)layout.findViewById(R.id.message)).setText(text);

builder
.setView(layout);


//builder.show(); commented this line
// I don't understand the purpose of this if block, anyways
// let it remain as is at the moment
if (dialog!=null){
dialog.cancel();
dialog.dismiss();
}

dialog = builder.create().show();


Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
dialog.cancel();
dialog.dismiss();
}
}, 500);


}

关于Android 在设置时间后隐藏对话框,就像自定义时间间隔 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131559/

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