AlertDialog 在没有处理程序的情况下获取返回值

发布于 2024-09-16 01:46:51 字数 1450 浏览 1 评论 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();
    }

编辑。回复 chris:

程序的执行应该等到用户选择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)

I would like to show a number of alertdialogs, so the user has to handle off some questions (a little like a wizard).

Is it possible to make the alertDialog wait until the user chooses something and then returning the choisen value?

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

EDIT. Reponse to chris:

The execution of the program should wait until the user chooses one of the options in the alertDialog

Is this possible?

Like this:

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-09-23 01:46:51

如果您希望出现一系列 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
                       }
               }) 

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

如果您要从 AlertDialog onClickListenerActivity 类进行一些回调,您可以使用 MyActivityClassName.this.myMethodOrVariableHere 这样做。

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

If you want a series of AlertDialog boxes to appear then your best bet would be to set some listeners inside the dialogs themselves.

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

Note that I am using the default positive button for the alert dialog, you may need to change this depending on what the user is clicking.

If you are going to be making some call backs into your Activity class from the AlertDialog onClickListener you can use MyActivityClassName.this.myMethodOrVariableHere to do so.

Hope that helps, leave me a comment if you want some more clarification.

从﹋此江山别 2024-09-23 01:46:51

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

I fixed it this way (pseudocode), because my code is to dirty :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());
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文