Android:ProgressDialog 然后 toast

发布于 2024-12-14 05:54:15 字数 2075 浏览 1 评论 0原文

可能的重复:
Android:ProgressDialog.show() 使用 getApplicationContext 崩溃

我想要取得进展首先显示对话框,然后显示吐司。我希望只要用户选择的时间延迟(例如 15、30、60 秒)加载进度对话框,并且没有延迟,然后显示消息已发送的 toast。我该如何实施?我在哪里以及如何在我的代码上执行此操作?

这是我的代码:

btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 
                boolean split = false;

                final Toast toast = Toast.makeText(getBaseContext(), 
                         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                          Toast.LENGTH_SHORT);

                Runnable showToastRunnable = new Runnable() {
                  public void run() {
                      toast.show();
        // Send button Listener

                  }
              };

                if (phoneNo.length()>0 && message.length()>0)  {
                    if (count == 0) {
                          handler.postDelayed(showToastRunnable, 0);
                      }
                      else if (count == 1) {
                          handler.postDelayed(showToastRunnable, 15000);
                      }
                      else if (count == 2) {
                          handler.postDelayed(showToastRunnable, 30000);
                      }
                      else if (count == 3) {
                          handler.postDelayed(showToastRunnable, 60000);
                      }
                }
                   // sendSMS(phoneNo, message, split); */
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

Possible Duplicate:
Android: ProgressDialog.show() crashes with getApplicationContext

I want to have a progress dialog to show up first then the toast. I want the progress dialog to load as long as the time delay chosen by the user like 15, 30, 60 seconds and no delay then the toast indicating that the message has been sent. How can I implement it? Where and how do I do it on my code?

Here's my code:

btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 
                boolean split = false;

                final Toast toast = Toast.makeText(getBaseContext(), 
                         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                          Toast.LENGTH_SHORT);

                Runnable showToastRunnable = new Runnable() {
                  public void run() {
                      toast.show();
        // Send button Listener

                  }
              };

                if (phoneNo.length()>0 && message.length()>0)  {
                    if (count == 0) {
                          handler.postDelayed(showToastRunnable, 0);
                      }
                      else if (count == 1) {
                          handler.postDelayed(showToastRunnable, 15000);
                      }
                      else if (count == 2) {
                          handler.postDelayed(showToastRunnable, 30000);
                      }
                      else if (count == 3) {
                          handler.postDelayed(showToastRunnable, 60000);
                      }
                }
                   // sendSMS(phoneNo, message, split); */
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

呆橘 2024-12-21 05:54:15

您可以使用以下代码显示进度对话框。

private ProgressDialog dialog;
public  void showProgress () {
    dialogSet = true;
    // prepare the dialog box
    //ProgressDialog 
    dialog = new ProgressDialog(this);
    // make the progress bar cancelable
    dialog.setCancelable(true);
    // set a message text
    dialog.setMessage("Please wait..");
    // show it
    dialog.show();

}

要取消对话框,您应该使用 dialog.cancel()。取消对话框后,可以显示Toast

像这样改变你的代码

Runnable showToastRunnable = new Runnable() {
              public void run() {
                  dialog.cancel();
                  toast.show();
              // Send button Listener
              }
             };
            if (phoneNo.length()>0 && message.length()>0)  {                                    
                showProgress ();    
                if (count == 0) {
                      handler.postDelayed(showToastRunnable, 0);

                  }
                  else if (count == 1) {
                      handler.postDelayed(showToastRunnable, 3000);

                  }
                  else if (count == 2) {
                      handler.postDelayed(showToastRunnable, 30000);
                  }
                  else if (count == 3) {
                      handler.postDelayed(showToastRunnable, 60000);
                  }
            }
               // sendSMS(phoneNo, message, split); */
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }

You can show progress dialog using following code

private ProgressDialog dialog;
public  void showProgress () {
    dialogSet = true;
    // prepare the dialog box
    //ProgressDialog 
    dialog = new ProgressDialog(this);
    // make the progress bar cancelable
    dialog.setCancelable(true);
    // set a message text
    dialog.setMessage("Please wait..");
    // show it
    dialog.show();

}

For cancelling the dialog, you should use dialog.cancel(). After cancelling dialog, you can display Toast

Change your code like this

Runnable showToastRunnable = new Runnable() {
              public void run() {
                  dialog.cancel();
                  toast.show();
              // Send button Listener
              }
             };
            if (phoneNo.length()>0 && message.length()>0)  {                                    
                showProgress ();    
                if (count == 0) {
                      handler.postDelayed(showToastRunnable, 0);

                  }
                  else if (count == 1) {
                      handler.postDelayed(showToastRunnable, 3000);

                  }
                  else if (count == 2) {
                      handler.postDelayed(showToastRunnable, 30000);
                  }
                  else if (count == 3) {
                      handler.postDelayed(showToastRunnable, 60000);
                  }
            }
               // sendSMS(phoneNo, message, split); */
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文