Android - ProgressDialog 完成后立即显示 AlertDialog/Toast
我对 Android 编程很陌生,所以请原谅我提出这个愚蠢的问题 >,< 正如标题所示,我想在 ProgressDialog 完成后立即显示 AlertDialog/Toast。我怎样才能以正确的方式做到这一点?注意:我的处理程序只是关闭 ProgressDialog (pd.dismissDialog())。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnDoRegister:
pd = ProgressDialog.show(RegisterActivity.this, "",
"Registering with the server..");
new Thread() {
public void run() {
Looper.prepare();
//(hopefully) thread-safe implementations here
handler.sendEmptyMessage(0);
}
}.start();
if (status == registered) {
//show AlertDialog/Toast here
finish();
} else if (status == notRegistered) {
//show AlertDialog/Toast here
}
break;
}
更令人困惑的是,当我尝试对此进行调试时,LogCat 没有显示任何内容,并且一切都运行良好(警报/toast 按预期显示)。但是当我尝试正常运行它时,不知怎的,我觉得它运行得太快并且没有正确显示我的警报/吐司(听起来很愚蠢)。非常感谢您的帮助。
I am quite new to Android programming, so please excuse me for the stupid question >,<
Just like the title says, I want to show a AlertDialog/Toast right after my ProgressDialog finishes. How I can do this in the proper way? Note: My handler is only to dismiss the ProgressDialog (pd.dismissDialog()).
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnDoRegister:
pd = ProgressDialog.show(RegisterActivity.this, "",
"Registering with the server..");
new Thread() {
public void run() {
Looper.prepare();
//(hopefully) thread-safe implementations here
handler.sendEmptyMessage(0);
}
}.start();
if (status == registered) {
//show AlertDialog/Toast here
finish();
} else if (status == notRegistered) {
//show AlertDialog/Toast here
}
break;
}
What is more confusing is, when I tried to debug this, LogCat shows nothing and everything was running perfectly (the alert/toast shows up as expected). But when I tried to run it normally, somehow I feel that this runs too fast and not showing my alert/toast correctly (sounds stupid). Thank you very much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在处理程序的handleMessage方法中显示AlertDialog/Toast会更合适,
It will be more suitable to show the AlertDialog/Toast in the handleMessage method of the handler,
发生这种情况是因为,当您运行线程时,它很快就会完成其工作。但是当你调试它时,它就像其他过程语言一样,你一个接一个地按顺序执行事情。但如果你仍然希望它长时间显示对话框,请执行此操作(尽管它没有实际用途)
It happens because, your thread is finishing its work soon when you run it. but when you debug its like other procedural languages where you execute things sequentially one after the other. but if you still want it show dialog for long time, do this (Though its of no Practical use)