AlertDialog获取没有处理程序的返回值

时间:2010-08-24 07:18:56

标签: java android return handler alertdialog

我想展示一些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

中的一个选项

这可能吗?

像这样:

    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)

2 个答案:

答案 0 :(得分: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 AlertDialogonClickListener课程打电话,可以使用MyActivityClassName.this.myMethodOrVariableHere这样做。

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

答案 1 :(得分: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());
                }
            }
        }
    }