0

我想展示一些警报对话框,所以用户必须处理一些问题(有点像向导)。

是否可以让 alertDialog 等到用户选择某些内容然后返回选择的值?

    HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position);

    int nextQuestion = position;

    while(nextQuestion != 0){
        Question question = questions.get(nextQuestion);

        CharSequence[] items = (String[])question.getAnswers();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(question.getQuestion());
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

            }
        });
        AlertDialog alert = builder.create();

        //I would like to do something like this:
        nextQuestion = alert.getClickedItem();
    }

编辑。回复克里斯:

程序的执行应该等到用户选择 alertDialog 中的选项之一

这可能吗?

像这样:

    private int answer = 0;

    ....
    ...methode(){

    //show dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(question.getQuestion());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            CallHandleMenu.this.answer = item; //setting answer
        }
    });
    builder.create().show();

    //Here I need to know the answer (the answer should not be 0 here)
4

2 回答 2

0

如果您希望出现一系列AlertDialog框,那么最好的办法是在对话框本身内设置一些侦听器。

.setPositiveButton("My Button Name", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           //do stuff like set some variables, maybe display another dialog
                       }
               }) 

请注意,我使用警报对话框的默认肯定按钮,您可能需要根据用户单击的内容更改此按钮。

Activity如果你打算从你的班级回电,AlertDialog onClickListener你可以MyActivityClassName.this.myMethodOrVariableHere这样做。

希望对您有所帮助,如果您需要更多说明,请给我留言。

于 2010-08-24T08:07:41.780 回答
0

我以这种方式修复它(伪代码),因为我的代码很脏:p

    activity{
        onCreate{
            makeNextQuestion(1)
        }

        public void nextQuestion(int questionId){
            //add string and answering buttons to layout
            btn.onClickList(){
                onClick(View btn){
                    activity.this.nextQuestion(btn.getId());
                }
            }
        }
    }
于 2010-08-24T14:53:00.590 回答