In mostly situation, I need the user to make a choice many times.( I do something and raise a message box for the user to make a choice and continue do other thing(maybe called block)) So I wrote a common function
public static void ShowMsgDialog(Context self,String title, String msg)
Although it correctly response the user’s action, but always pending (that means while I click the button, the previous action's value is visible by global variable’s value) Is there exist any function which I could got the message box's return value and use it like this:
int ret = ShowMsgDialog(Context self,String title, String msg);
the follow is my code:
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();
}
}
Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
I don't think that there is any way to get a value from an alertDialog like this
int ret = ShowMsgDialog(Context self,String title, String msg);
because by the time your dialog will be shown the onClick() of your Button will have already finished.
So i suggest using another way to implement this.
Since the method to create the alertDialog is inside your activity it is as easy as creating a function in your activity like below:
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();
}
and call your method in onClick()
like this:
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With