在大多数情况下,我需要用户多次做出选择。(我做某事并提出一个消息框让用户做出选择并继续做其他事情(可能称为块))所以我写了一个通用函数
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();
}
}