如何在AlertDialog中设置单项选择项?

发布于 2024-12-11 08:58:56 字数 1433 浏览 0 评论 0原文

我无法在 AlertDialog 中设置单项选择列表或多项选择列表。

我尝试遵循这些示例,但只得到一个带有标题、“确定”和“取消”按钮的对话框,没有列表,也没有消息(我设置的!)。

这是代码:

    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DELETE_CITY:
        CharSequence[] array = {"Red", "Blue", "Yellow"}; 
        return new AlertDialog.Builder(ShowPypData.this)
            .setTitle(R.string.city_actions_delete_label)
            .setMessage(R.string.city_actions_delete_select_label)
            .setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }

            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            }).create();
    default:
        return null;
    }

}

奇怪的是,如果我注释 setSingleChoiceItems 部分,我可以在对话框上看到消息。

I haven't been able to set a Single Choice list, or a Multiple Choice List inside an AlertDialog.

I tried following the examples but I only get a Dialog with a Title, the Ok and Cancel buttons, and no list, and NO message (which I set!).

Here is the code:

    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DELETE_CITY:
        CharSequence[] array = {"Red", "Blue", "Yellow"}; 
        return new AlertDialog.Builder(ShowPypData.this)
            .setTitle(R.string.city_actions_delete_label)
            .setMessage(R.string.city_actions_delete_select_label)
            .setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }

            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            }).create();
    default:
        return null;
    }

}

The weird thing is that if I comment the setSingleChoiceItems part, I can see the message on the dialog.

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

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

发布评论

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

评论(2

女中豪杰 2024-12-18 08:58:56

似乎 ButtonsMessageMultiple choice items 是互斥的。尝试注释掉setMessage()setPositiveButton()setNegativeButton()。我自己没检查过。

Seems that Buttons, Message and Multiple choice items are mutually exclusive. Try to comment out setMessage(), setPositiveButton() and setNegativeButton(). Didn't check it myself.

泪冰清 2024-12-18 08:58:56

这段代码对我有用

 final CharSequence[] charSequence = new CharSequence[] {"As Guest","I have account here"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Buy Now")
                //.setMessage("You can buy our products without registration too. Enjoy the shopping")
                .setSingleChoiceItems(charSequence, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        utility.toast(" "+charSequence);
                    }
                })
        .setPositiveButton("Go", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show()

this code works for me

 final CharSequence[] charSequence = new CharSequence[] {"As Guest","I have account here"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Buy Now")
                //.setMessage("You can buy our products without registration too. Enjoy the shopping")
                .setSingleChoiceItems(charSequence, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        utility.toast(" "+charSequence);
                    }
                })
        .setPositiveButton("Go", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文