在大多数情况下,我需要用户多次做出选择。(我做了一些事情并为用户做了一个消息框以做出选择并继续做其他事情(可能称为阻止)) 所以我写了一个共同的函数
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();
}
}
答案 0 :(得分:9)
我认为没有办法从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);
}
});
答案 1 :(得分:0)
您需要在正面和负面按钮Click事件中显示toast。因为当您单击按钮时会显示警报,之后如果条件执行,则您从上次打开的警报视图按钮中获得值。所以这是错的。试试这个:
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();
}
答案 2 :(得分: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;
}