Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the AlertDialog's return value

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



}
like image 420
George Avatar asked Jul 27 '13 04:07

George


People also ask

How do I use AlertDialog?

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.

What is the use of AlertDialog in Android?

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.

What is AlertDialog message?

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.

What is the difference between dialog and AlertDialog?

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 .


1 Answers

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);
            }
        });
like image 102
Manolis Proimakis Avatar answered Oct 15 '22 11:10

Manolis Proimakis