【问题标题】:Android: ProgressDialog then toast [duplicate]Android:ProgressDialog 然后吐司 [重复]
【发布时间】:2011-11-08 04:46:12
【问题描述】:

可能重复:
Android: ProgressDialog.show() crashes with getApplicationContext

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

这是我的代码:

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();
            }
        });        
    }

【问题讨论】:

  • 你的进度对话框代码在哪里?
  • 没有,不知道怎么实现

标签: android delay progressdialog toast


【解决方案1】:

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

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();
        }

【讨论】:

  • 我的意思是当用户点击发送按钮时我想要一个进度对话框。例如,用户在点击发送按钮时选择了 15 秒延迟,15 秒后将显示进度对话框,toast 将显示消息已发送。
  • 首先调用showProgress(),它将显示进度对话框,然后在调用方法后延迟15秒,延迟15秒后取消对话框并显示toast。
  • 你能用我的代码给我看吗?从上面?并在其中应用您的代码?
  • dialogSet 无法解析为变量
  • 删除该语句,它是一个布尔变量
猜你喜欢
  • 1970-01-01
  • 2013-12-20
  • 2018-09-13
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多