如何使用AlertDialog的返回值 [英] How to use the AlertDialog's return value

查看:530
本文介绍了如何使用AlertDialog的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多数情况下,我需要用户作出选择了很多次。(我做了什么,并提出一个消息框供用户做出选择,继续做其他的事情(也许称为块))
所以我写了一个共同的功能

 公共静态无效ShowMsgDialog(上下文自我,弦乐标题,弦乐味精)

虽然正确地响应用户的操作,但总是挂起(当我按一下按钮,这意味着,在previous行动的价值是全球变量的值可见)
是否存在,我可以得到的消息框的返回值的功能,使用它是这样的:

  INT RET = ShowMsgDialog(上下文自我,弦乐标题,弦乐味精);

后续是我的code:

 公共类MainActivity延伸活动{
    私人按钮BUTTON1;
    枚举答案{YES,NO ERROR};
    静态回答选择;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        BUTTON1 =(按钮)findViewById(R.id.button1);        button1.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                ShowMsgDialog(MainActivity.this,信息,你的选择吗?);
                如果(选择== Answer.YES)
                    Toast.makeText(MainActivity.this,你CHOICED YES,Toast.LENGTH_LONG).show();
                否则,如果(选择== Answer.NO)
                    Toast.makeText(MainActivity.this,你CHOICED否,Toast.LENGTH_LONG).show();
                其他
                    Toast.makeText(MainActivity.this,ERROR OCUS,Toast.LENGTH_LONG).show();                // INT RET = ShowMsgDialog(MainActivity.this,信息,你的选择吗?);
            }
        });
    }    公共静态无效ShowMsgDialog(上下文自我,标题字符串,字符串消息){
        AlertDialog.Builder dlgAlert =新AlertDialog.Builder(个体经营);
        dlgAlert.setTitle(职称);
        dlgAlert.setMessage(MSG);
        dlgAlert.setPositiveButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton){
                //调用你的code在这里
                选择= Answer.YES;
            }
        });
        dlgAlert.setNegativeButton(取消,新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                // TODO自动生成方法存根
                选择= Answer.NO;
            }
        });
        dlgAlert.show();
    }}


解决方案

我不认为有任何办法从alertDialog得到一个值
像这样

  INT RET = ShowMsgDialog(上下文自我,弦乐标题,弦乐味精);

因为当你的对话框将显示onClick的()你的按钮将已经完成。

所以我建议用另一种方式来实现这一点。

由于创建alertDialog的方法是你的活动里面是像下面您的活动创造一个功能简单的:

 公共无效userChose(字符串的choise){         如果(选择== Answer.YES)
            //你的code表示是这里
            Toast.makeText(MainActivity.this,你选择YES,Toast.LENGTH_LONG).show();
         否则,如果(选择== Answer.NO)
            //你的code。对于无法在这里
            Toast.makeText(MainActivity.this,选择否,Toast.LENGTH_LONG).show();}

和调用您的方法的onClick()

是这样的:

  dlgAlert.setPositiveButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton){
                userChose(Answer.YES);
            }
        });
        dlgAlert.setNegativeButton(取消,新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                userChose(Answer.NO);
            }
        });

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



}

解决方案

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

这篇关于如何使用AlertDialog的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆