8

在大多数情况下,我需要用户多次做出选择。(我做某事并提出一个消息框让用户做出选择并继续做其他事情(可能称为块))所以我写了一个通用函数

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



}
4

3 回答 3

11

我认为没有任何方法可以从这样的 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);
            }
        });
于 2013-07-27T05:22:41.877 回答
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();
    }
于 2013-07-27T05:50:45.860 回答
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;
}
于 2015-07-19T15:19:12.687 回答