带选择器的 AlertDialog

发布于 2024-12-22 15:13:44 字数 325 浏览 2 评论 0原文

我正在尝试使用看起来完全像这样的选择器创建一个对话框:

AlertDialog with selector

我尝试过使用 AlertDialog它包含一个 ListView,但在 ListView 和底部灰色区域之间给出了丑陋的黑色边框。我可以使用普通的对话框,但我不想手动构建底部灰色区域。

我知道我可以对 AlertDialog 进行子类化,但随后我还需要对 Builder 进行子类化,最终会为这样一个小细节编写大量代码。有什么巧妙的方法可以做到这一点吗?

干杯,

I am trying to do a Dialog with a selector that looks exactly like this:

AlertDialog with selector

I've tried using an AlertDialog that holds a ListView, but that gives an ugly black border between the ListView and the bottom gray area. I could use a normal Dialog, but I don't want to build the bottom gray area manually.

I know that I can subclass the AlertDialog, but then I will also need to subclass the Builder and it ends up being a lot of code for such a small detail. Is there any neat way of doing this?

Cheers,

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

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

发布评论

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

评论(2

默嘫て 2024-12-29 15:13:44

使用警报对话框生成器,它有相应的选项。简短示例:

AlertDialog.Builder adb = new AlertDialog.Builder(this);
CharSequence items[] = new CharSequence[] {"First", "Second", "Third"};
adb.setSingleChoiceItems(items, 0, new OnClickListener() {

        @Override
        public void onClick(DialogInterface d, int n) {
            // ...
        }

});
adb.setNegativeButton("Cancel", null);
adb.setTitle("Which one?");
adb.show();

请参阅对话框文档添加部分一个列表。

Use the alert dialog builder, it has options for that. Short example:

AlertDialog.Builder adb = new AlertDialog.Builder(this);
CharSequence items[] = new CharSequence[] {"First", "Second", "Third"};
adb.setSingleChoiceItems(items, 0, new OnClickListener() {

        @Override
        public void onClick(DialogInterface d, int n) {
            // ...
        }

});
adb.setNegativeButton("Cancel", null);
adb.setTitle("Which one?");
adb.show();

See the dialogs doc, section Adding a list.

与酒说心事 2024-12-29 15:13:44

您应该使用以下代码来选择单个项目。这是工作代码

CharSequence colors[] = new CharSequence[]{"View PDF", "Reduce Size", "Delete PDF", "Share PDF"};

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Select Option");
            builder.setItems(colors, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.e("value is", "" + which);
                    switch (which) {
                        case 0:
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                    }
                }
            });
            builder.show();

You Should Use following code to select single item. This is working code

CharSequence colors[] = new CharSequence[]{"View PDF", "Reduce Size", "Delete PDF", "Share PDF"};

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Select Option");
            builder.setItems(colors, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.e("value is", "" + which);
                    switch (which) {
                        case 0:
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                    }
                }
            });
            builder.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文