2014-01-11 25 views
0

我創建了Aler Dialog onCreate我的班級功能。通過按下正向按鈕,對話框將運行一個AsyncTask,其中onPostExecute運行一個功能,該功能應該關閉對話創建的onCreate功能。如何關閉創建時顯示的Android對話框,從功能

我的問題是該函數應該如何能夠取消由其他函數創建的對話框? 警告對話框CODE:

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

謝謝!

回答

0

您只需在課堂級別保留對onCreate()方法以外的AlertDialog的引用。在onCreate()show()對話框中,從你的方法你可以dismiss()它。

public class Abc extends Activity { 

AlertDialog alert; 

    onCreate(){ 
     // initialise the AlertDialog, then show it 
     alert.show(); 
    } 

    otherMethod() { 
     alert.dismiss(); 
    } 




} 
+0

也是其他小問題無法啓動的其他話題,我要如何改變ontherMethod()TextView的提示(從對話框)它對話框shouwn? – Iosif

+0

我猜想同樣的事情,請在課堂級別保留對TextView或EditText的引用。我不確定是否可以在對話框顯示時更改它們。 – FWeigl

相關問題