具有自定义单元格样式的Android AlertDialog.Builder setSingleChoiceItems [英] Android AlertDialog.Builder setSingleChoiceItems with custom cell style

查看:458
本文介绍了具有自定义单元格样式的Android AlertDialog.Builder setSingleChoiceItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义单元格,以删除该单元格右侧的按钮".

I want to create a custom cell that remove the "button" in the right hand side of the cell.

对于AlertDialog,通过此链接,我夸大了xml中的单元格,但它仅显示在setSingleChoiceItems的listView外部.

for the AlertDialog, from this link, I inflated the cell from xml but it only appear outside the listView of the setSingleChoiceItems.

我的代码:

AlertDialog.Builder builder;

    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog));
    } else {
        builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth));
    }

    final CharSequence[] choiceList = {
            getActivity().getResources().getString(R.string.opt_remind),
            getActivity().getResources().getString(R.string.opt_calendar)};

    builder.setSingleChoiceItems(
            choiceList, 
            -1, // does not select anything
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int index) {
                    switch (index) {
                    case 0: // remind me
                        //
                        break;
                    case 1: // add to calendar
                        //
                        break;
                    default:
                        break;
                    }
                    dialog.dismiss();
                }
            });
    builder.setCancelable(true);
    builder.setNegativeButton(getActivity().getResources().getString(R.string.opt_cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

感谢您的帮助!

最诚挚的问候, 锡苏斯

Best regards, Sythus

推荐答案

您需要使用适配器,而不是将CharSequence数组直接传递给builder.setSingleChoiceItems.像这样

Instead of passing the CharSequence Array directly to builder.setSingleChoiceItems, you need to use an Adapter. Like this

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
    getActivity(), R.layout.your_custom_view, choiceList);

builder.setSingleChoiceItems(adapter, -1, new OnClickListener() { ... });

然后您可以在your_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="?android:attr/textColorAlertDialogListItem"
    android:gravity="center_vertical"
    android:paddingStart="16dip"
    android:paddingEnd="7dip"
    android:ellipsize="marquee" />

这样,您将获得与现在一样的默认样式.如果您想要复选标记或要对其进行自定义,则默认值为

This way you get the default style like you do have now. If you want the check mark back or want to customize it, the default value is

    android:checkMark="?android:attr/listChoiceIndicatorSingle"

这篇关于具有自定义单元格样式的Android AlertDialog.Builder setSingleChoiceItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆