gpt4 book ai didi

具有自定义单元格样式的Android AlertDialog.Builder setSingleChoiceItems

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:31 24 4
gpt4 key购买 nike

我想创建一个自定义单元格,删除单元格右侧的“按钮”。

image1 image2

对于 AlertDialog,来自 link ,我从 xml 中膨胀了单元格,但它只出现在 setSingleChoiceItems 的 listView 之外。

我的代码:

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

感谢您的帮助!

最好的问候,西修斯

最佳答案

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

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" />

这样您就可以像现在一样获得默认样式。如果你想要复选标记回来或者想自定义它,默认值是

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

关于具有自定义单元格样式的Android AlertDialog.Builder setSingleChoiceItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com