如何从函数中关闭create上显示的Android Dialog

时间:2014-01-11 13:02:35

标签: android dialog android-asynctask

我创建了我班级的Aler Dialog onCreate功能。通过按下肯定按钮,对话框将运行AsyncTask onPostExecute运行一个功能,该功能应该关闭创建的对话框onCreate功能。

我的问题是该函数应该如何能够取消由其他函数创建的对话框? 警报对话框代码:

AlertDialog.Builder builder_t = new AlertDialog.Builder(PilotLogbook_main.this);
                                            final EditText code = new EditText(PilotLogbook_main.this);
                                            code.setHint("Activation Code");

                                            builder_t.setView(code);
                                            builder_t.setTitle("First Start");
                                            builder_t.setMessage(Html.fromHtml(
                                                    "ITB Studios sent an activation code to supplied email."
                                                    + "<br><br>"
                                                    + "Enter Activation Code:" ))
                                                   .setCancelable(false)
                                                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        finish();
                                                    }
                                                   })
                                                   .setPositiveButton("Finish", new DialogInterface.OnClickListener() {
                                                       public void onClick(DialogInterface dialog, int id) {
                                                       }
                                                   });


                                            final AlertDialog alert_t = builder_t.create();
                                            alert_t.show();
                                        alert_t.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
                                      {

                                          @Override
                                          public void onClick(View v)
                                          { 

                                              if(code.getText().toString().length() < 1)
                                                 code.setError("Activation Code Is Requierd!");


                                              else
                                                  { 
                                                if(InternetConnectionStatus.getInstance(PilotLogbook_main.this).isOnline(PilotLogbook_main.this))
                                                    {




                                                    new GetData().execute("URL.which.echo.true.or.false");






                                                        //
                                                    }
                                                else
                                                {
                                                    email.setError("No Internet Connection! Please make sure you have internet connection and restart Pilot Logbook.");
                                                }

                                                  }




                                          }
                                      });

GetData异步代码:

private final class GetData extends AsyncTask<String, Void, String> {


         protected String doInBackground(String... message) {
            HttpClient httpclient;
            HttpGet request;
            HttpResponse response = null;
            String result = "";
            // TextView to display result

            // Try to connect using Apache HttpClient Library
            try {
                httpclient = new DefaultHttpClient();
                request = new HttpGet(message[0]);
                response = httpclient.execute(request);
            }

            catch (Exception e) {
                // Code to handle exception
                //result = "error";
            }

            // response code
            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {

                    // Appending result to textview
                    result = result + line ;
                }
            } catch (Exception e) {
                // Code to handle exception
                result = "";
            }
            return result;
        }

        protected void onPostExecute(String result) {
            Log.w("!PHP!", result);
            someMethod(result);
        }

        }

Somemethod功能代码(这应取消现有的对话框):

public void someMethod(String php) {
        if(php.equals("True"))
        {
            Toast.makeText(PilotLogbook_main.this, "TRUE", Toast.LENGTH_LONG).show();
        /****************** HERE IS THE QUESTION, HOW THE FUNCTION SHOULD LOOKS LIKE? *******/

        }
        else
        {
            Toast.makeText(PilotLogbook_main.this, "FALSE", Toast.LENGTH_LONG).show();
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须在课程级别保留对AlertDialog方法之外的onCreate()的引用。在onCreate() show()对话框中,您可以dismiss()从方法中public class Abc extends Activity { AlertDialog alert; onCreate(){ // initialise the AlertDialog, then show it alert.show(); } otherMethod() { alert.dismiss(); } }

喜欢

{{1}}