Android - ProgressDialog 完成后立即显示 AlertDialog/Toast

发布于 2024-12-04 04:07:38 字数 1029 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2024-12-11 04:07:38

在处理程序的handleMessage方法中显示AlertDialog/Toast会更合适,

static Handler handler = new Handler() {
       public void handleMessage(Message msg) { ...  }};

It will be more suitable to show the AlertDialog/Toast in the handleMessage method of the handler,

static Handler handler = new Handler() {
       public void handleMessage(Message msg) { ...  }};
梦初启 2024-12-11 04:07:38

发生这种情况是因为,当您运行线程时,它很快就会完成其工作。但是当你调试它时,它就像其他过程语言一样,你一个接一个地按顺序执行事情。但如果你仍然希望它长时间显示对话框,请执行此操作(尽管它没有实际用途)

new Thread() {
                public void run() {
                    Looper.prepare();
                    //(hopefully) thread-safe implementations here
                    handler.sendEmptyMessage(0);
                     Thread.sleep(3000);
                }
            }.start();

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)

new Thread() {
                public void run() {
                    Looper.prepare();
                    //(hopefully) thread-safe implementations here
                    handler.sendEmptyMessage(0);
                     Thread.sleep(3000);
                }
            }.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文