如何使用AlertDialog的返回值

在大多数情况下,我需要用户多次做出选择。(我做了一些事情并为用户做了一个消息框做出选择并继续做其他事情(可能称为块))所以我写了一个常用的函数

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

虽然它正确响应了用户的操作,但总是挂起(这意味着当我单击按钮时,前一个操作的值可以通过全局variables的值看到)是否存在任何可以获取消息框的返回值并使用它的函数:

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

我认为没有办法从这样的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); } }); 

您需要在正面和负面按钮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(); } 

虽然您无法直接从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 mActivityWeakReference; IncomingHandler(MainActivity activity) { mActivityWeakReference = new WeakReference(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; }