Android 在设置时间后隐藏对话框,就像自定义时间间隔 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);
}
I'm trying to display some text on the screen for a set period of time, similar to a toast, but with the ability to specify the exact time it is on the screen. I'm thinking an alert dialog may work for this, but I can't seem to figure out how to dismiss the dialog automatically.
Can you suggest an alternative to toast notifications in which I can specify the exact time it is displayed?
Thank you!
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要计时调用
Dialog#dismiss()
;对于你的问题Handler
类就足够了(+1 到 Javanator :))。仅供参考,还有其他类,即 AlarmManager、计时器 & TimerTask 可以帮助计时代码的运行。
编辑:
将您的代码更改为:
You just need to time calls to
Dialog#dismiss()
; for your problemHandler
class would suffice(+1 to Javanator :)).FYI, there are other classes namely AlarmManager, Timer & TimerTask that can help with timing the runs of your code.
EDIT:
Change your code to:
在您的代码需要关闭对话框的地方执行类似的操作。
希望这有帮助:)
Do something like this where ever your code need to dismiss the dialog.
Hope This Help :)