如何使用AlertDialog的返回值

Geo*_*rge 6 android synchronization

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

public static void ShowMsgDialog(Context self,String title, String msg)
Run Code Online (Sandbox Code Playgroud)

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

int ret = ShowMsgDialog(Context self,String title, String msg);
Run Code Online (Sandbox Code Playgroud)

以下是我的代码:

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



}
Run Code Online (Sandbox Code Playgroud)

Man*_*rom 10

我认为没有办法从这样的alertDialog中获取值

int ret = ShowMsgDialog(Context self,String title, String msg);
Run Code Online (Sandbox Code Playgroud)

因为当你的对话框显示时,你的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();

}
Run Code Online (Sandbox Code Playgroud)

并在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);
            }
        });
Run Code Online (Sandbox Code Playgroud)