Java AlertDialog获取不带处理程序的返回值

Java AlertDialog获取不带处理程序的返回值,java,android,return,handler,android-alertdialog,Java,Android,Return,Handler,Android Alertdialog,我想显示一些alertdialogs,因此用户必须像向导一样处理一些问题 是否可以让alertDialog等待用户选择某个内容,然后返回choisen值 HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position); int nextQuestion = position; while(nextQuestion != 0){ Question

我想显示一些alertdialogs,因此用户必须像向导一样处理一些问题

是否可以让alertDialog等待用户选择某个内容,然后返回choisen值

    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 Box,那么最好在对话框内部设置一些侦听器

.setPositiveButton("My Button Name", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           //do stuff like set some variables, maybe display another dialog
                       }
               }) 
请注意,我正在使用警报对话框的默认积极按钮,您可能需要根据用户单击的内容进行更改

如果要从AlertDialog onClickListener对Activity类进行回调,可以使用MyActivityClassName.this.myMethodOrVariableHere执行此操作


希望这能有所帮助,如果您想进一步澄清,请给我留言。

我用伪代码的方式修复了它,因为我的代码太脏了: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());
                }
            }
        }
    }

@Berty不希望答案出现在AlertDialog的onClickListener之外。而是将其保存在那里,并使用showDialog int-id或showDialog int-id、Bundle-args启动下一个对话框。这两种方法都是Activity类的方法,因此CallHandleMenu.this.showDialogargs是您要查找的
    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());
                }
            }
        }
    }