2013-07-27 64 views
5

在大多數情況下,我需要用戶多次作出選擇(我做了一些事情併爲用戶提出一個消息框以供選擇並繼續做其他事情(也許稱爲塊)) 所以我寫了一個共同的功能如何使用AlertDialog的返回值

public static void ShowMsgDialog(Context self,String title, String msg) 

雖然正確地響應用戶的動作,但始終懸而未決(這意味着當我按一下按鈕,之前的動作的價值是全局變量的值,可見光) 有存在任何我可以獲得消息框的返回值並使用它的功能:

int ret = ShowMsgDialog(Context self,String title, String msg); 

後續是我的代碼:

public class MainActivity extends Activity { 
    private Button button1; 
    enum Answer { YES, NO, ERROR}; 
    static Answer choice; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  
     button1 = (Button)findViewById(R.id.button1); 

     button1.setOnClickListener(new OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       ShowMsgDialog(MainActivity.this, "Information", "you choice? "); 
       if(choice == Answer.YES) 
        Toast.makeText(MainActivity.this, "YOU CHOICED YES", Toast.LENGTH_LONG).show(); 
       else if (choice == Answer.NO) 
        Toast.makeText(MainActivity.this, "YOU CHOICED NO", Toast.LENGTH_LONG).show(); 
       else 
        Toast.makeText(MainActivity.this, "ERROR OCUS", Toast.LENGTH_LONG).show(); 

       //int ret = ShowMsgDialog(MainActivity.this, "Information", "you choice? "); 
      } 
     });  
    } 

    public static void ShowMsgDialog(Context self,String title, String Msg){ 
     AlertDialog.Builder dlgAlert = new AlertDialog.Builder(self); 
     dlgAlert.setTitle(title); 
     dlgAlert.setMessage(Msg); 
     dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       // call your code here 
       choice = Answer.YES; 
      } 
     }); 
     dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       choice = Answer.NO; 
      } 
     }); 
     dlgAlert.show(); 
    } 



} 

回答

7

我不認爲有任何方式從alertDialog 得到的值這樣

int ret = ShowMsgDialog(Context self,String title, String msg); 

因爲當你的對話框將顯示你的Button的onClick()已經完成。

所以我建議使用另一種方式來實現這一點。

自創建alertDialog的方法是你的活動裏面是象下面這樣的活動創建一個功能簡單:

public void userChose(String choise){ 

     if(choice == Answer.YES) 
      //YOUR CODE FOR YES HERE 
      Toast.makeText(MainActivity.this, "YOU CHOSE YES", Toast.LENGTH_LONG).show(); 
     else if (choice == Answer.NO) 
      //YOUR CODE FOR NO HERE 
      Toast.makeText(MainActivity.this, "YOU CHOSE NO", Toast.LENGTH_LONG).show(); 

} 

,並致電中的onClick()方法

這樣:

 dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       userChose(Answer.YES); 
      } 
     }); 
     dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       userChose(Answer.NO); 
      } 
     }); 
+0

在不同的功能,我打算用函數的返回值。如果使用全局變量,那麼我不知道 – George

+0

哪個函數調用的值取得了答案變量的值,我不知道 – George

+0

我不明白你的意思是抱歉,你能寫得更好嗎? :S,但我相信獲得返回值的唯一有效解決方案是調用另一個函數,它將根據您的需要更新您的UI,因爲當您顯示AlertDialog時,無法確定何時會關閉對話框從用戶,你不能只是暫停UI線程,並等待,直到從alertDialog返回值。所以你需要使用像我的例子。 – ManosProm

0

你需要顯示在敬酒的正面和負面按鈕的Click事件。因爲當你點擊按鈕時,警報出現,之後如果條件執行,以便你從上次打開的警報視圖按鈕點擊有價值。所以它是錯的。試試這個:

public static void ShowMsgDialog(Context self,String title, String Msg){ 
     AlertDialog.Builder dlgAlert = new AlertDialog.Builder(self); 
     dlgAlert.setTitle(title); 
     dlgAlert.setMessage(Msg); 
     dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       // call your code here 
       choice = Answer.YES; 
       Toast.makeText(MainActivity.this, "YOU CHOICED YES", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       choice = Answer.NO; 
       Toast.makeText(MainActivity.this, "YOU CHOICED NO", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     dlgAlert.show(); 
    } 
+0

Toast a message is a sample,realities我使用其他代碼來代替它,我的意思是使用AlertDialog的返回值。 – George

+0

如果不點擊它,您無法獲得alertbutton點擊的返回值。所以你需要做一些警報按鈕點擊。使用警報按鈕值調用任何按鈕點擊功能。 –

0

雖然你不能直接從AlertDialog返回一個值,你可以在答案發送到活動的處理程序:

public static void ShowMsgDialog(Context self,String title, String Msg){ 
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(self); 
    dlgAlert.setTitle(title); 
    dlgAlert.setMessage(Msg); 
    dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      // call your code here 
      Message msg = Message.obtain(); 
      msg.arg1 = HandlerMessage.Yes.ordinal(); 
      mHandler.sendMessage(msg);     
     } 
    }); 
    dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Message msg = Message.obtain(); 
      msg.arg1 = HandlerMessage.No.ordinal(); 
      mHandler.sendMessage(msg); 
     } 
    }); 
    dlgAlert.show(); 
} 

enum HandlerMessage 
{ 
    Yes, 
    No 
} 

/** for posting authentication attempts back to UI thread */ 
private final IncomingHandler mHandler = new IncomingHandler(this); 

static class IncomingHandler extends Handler 
{ 
    private final WeakReference<MainActivity> mActivityWeakReference; 

    IncomingHandler(MainActivity activity) 
    { 
     mActivityWeakReference = new WeakReference<MainActivity>(activity); 
    } 

    @Override 
    public void handleMessage(Message msg) 
    { 
     MainActivity activity = mActivityWeakReference.get(); 
     if (activity != null) 
     { 
      activity.HandleMessage(msg); 
     } 
    } 
} 
public void HandleMessage(Message msg) 
{ 
    HandlerMessage handlerMessage = HandlerMessage.values()[msg.arg1]; 

    switch(handlerMessage){ 
     case Yes: 
       //Do something ... 
       break; 
      case No: 
       //Do something ... 
       break; 
}