android如何显示和删除alertdialog

时间:2012-08-12 23:48:40

标签: android email alertdialog

在我的android中,我想显示一个无法退出的消息框,并且在显示时,需要运行另一个进程(在我的情况下是一个电子邮件发送功能)。然后在电子邮件发送完毕后,警报框需要关闭。

这是我到目前为止所得到的,但它不起作用......

有人可以帮忙吗?

    AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
    alertDialog.setTitle("Sending...");
    alertDialog.setMessage("Please wait while your details and image is being sent to x.");

    alertDialog.show(); // show the alert box
    Reusable_CodeActivity.send_email(gmailAC, gmpassword, get_all_details(), image_path, this);

    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        } 
    });

2 个答案:

答案 0 :(得分:2)

以下是您可能使用的AsyncTask的简单结构:

private class SendEmailTask extends AsyncTask<Void, Void, Void> {     
   protected void onPreExecute() {
      //Show the dialog first
   }
   protected void doInBackground(Void... params) {
      //Send Email Code
   }

   protected void onPostExecute(Void result) {
      //Dismiss the dialog 
   }
}

答案 1 :(得分:2)

我不确定电子邮件发送部分,但我可以帮助您按照自己的意愿制作消息框。

如果删除以下代码:

alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    } 
});

然后对话框将显示没有按钮,如果您添加.setCancelable(false),则在您使用alertDialog.cancel();

告知之前,它不会被解除

这是一个例子(从我的一个对话框中修改):

AlertDialog.Builder builder = new AlertDialog.Builder(this); // Create the dialog object
        builder.setMessage(R.string.dialog_disclaimer_text)  // I use a reference to a string resource - it's good practice instead of using hardcoded text
               .setIcon(android.R.drawable.ic_dialog_info)   // Here I specify an icon to be displayed in the top corner
               .setTitle(R.string.dialog_disclaimer_title)
               .setCancelable(false) // This one makes the dialog stay until the dismiss is called

               .create().show(); // Show the dialog

此代码将显示一个带有文本的对话框,该对话框在活动调用builder.dismiss();之前不会消失 - 您必须在某种类型的侦听器中实现它,或者在发送完成后回调。< / p>

更新

看看另一个答案,这可能是你的代码应该是这样的(感谢iturki)

private class SendEmailTask extends AsyncTask<Void, Void, Void> {  
    AlertDialog.Builder alertDialog; // Define the AlertDialog builder object so it can be used/adressed across the entire class

    protected void onPreExecute() {
       //Show the dialog first
       alertDialog = new Builder(context);
       alertDialog.setTitle("Sending...")
                  .setMessage("Please wait while your details and image is being sent to x.");
                  .setCancelable(false)
                  .show();
    }
    protected void doInBackground(Void... params) {
       //Send Email Code
       Reusable_CodeActivity.send_email(gmailAC, gmpassword, get_all_details(), image_path, this);
    }

    protected void onPostExecute(Void result) {
       //Dismiss the dialog 
       alertDialog.dismiss();
    }
}
相关问题