我想向此标准 AlertDialog 添加一个复选框,并带有单个选择列表
new AlertDialog.Builder(this)
.setSingleChoiceItems(R.array.difficultyLevel_list, 1, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) { level = which; }
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
现在,它显示得很好:
我转向使用自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/levels_keep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/levels_keep" />
<ListView
android:id="@+id/levels_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/difficultyLevel_list"
android:choiceMode="singleChoice" />
</LinearLayout>
使用此代码:
LayoutInflater inflater = getLayoutInflater();
final View customView = inflater.inflate(R.layout.dialog_levels, null);
final ListView list = (ListView) customView.findViewById(R.id.levels_list);
final CheckBox keep = (CheckBox) customView.findViewById(R.id.levels_keep);
// list.setSelected(level);
list.setItemChecked(level, true); // Marks the item as checked
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
level = position;
}
});
new AlertDialog.Builder(this)
.setView(customView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CheckBox keep = (CheckBox) findViewById(R.id.levels_keep);
if( keep.isChecked() ) {
Context context = PlayersActivity.this;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sp.edit();
editor.putString(context.getString(R.string.pref_level_key), String.valueOf(level));
editor.commit();
}
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
其中显示(列表可滚动):
有几件事我无法弄清楚:
- 为什么我在列表中看不到单选按钮?
如何设置默认选中项?(使用解决方案编辑代码)如何监听选择中的更改?(编辑代码,因为我找到了如何做到这一点)- 如何在复选框方 block 和复选框文本之间添加填充。我尝试了 android:paddingLeft 但这让情况变得更糟,文本和正方形重叠。我尝试了 android:drawablePadding 没有效果。
- 为什么主题改变了? (不是很重要,只是好奇)编辑似乎主题颜色都搞乱了,当我单击一个项目时,它的文本颜色变成黑色并“消失”到行的背景中
最佳答案
Why I don't see radio buttons on the list?
看来设置 android:entries
属性使得ListView
创建 ArrayAdapter
使用com.android.internal.R.layout.simple_list_item_1
作为布局。我不知道该布局是否完全复制 android.R.layout.simple_list_item_1
布局,但如果确实如此,那么它只是一个 TextView
所以不CheckBox
可用。
How do I set a default checked item?
如果我上面说的是真的,那么删除 entries
属性并设置 *levels_list* ListView
的适配器在代码中使用android.R.layout.simple_list_item_single_choice
作为行布局。
How do I listen to changes in the selection?
设置OnItemClickListener
关于ListView
从布局来看。
How can I add padding between the checkbox square and the checkbox text. I tried android:paddingLeft but is makes it worse, overlapping text and square. I tried android:drawablePadding to no effect.
使用简单的CheckBox
和一个 TextView
而不仅仅是 CheckBox
你可以随意放置它们。
Why the theme has changed ? (no really important, just curious)
我不确定。首先,正如我上面所说的ListView
使用特殊的布局(我不知道它的样式如何)并且您使用自定义 View 而不是正常的 ListView
内容又可以采用不同的样式。
如果你只是想要一个ChekcBox
在标题中,为什么不只是 set a custom title关于AlertDialog
并继续使用setSingleChoiceItems
?
由OP使用解决方案进行编辑
我在这里发布了工作代码,因为它绝对是基于 Luksprog 答案,我想将其标记为答案。该代码经过测试并且运行良好。
LayoutInflater inflater = getLayoutInflater();
final View customView = inflater.inflate(R.layout.dialog_levels, null);
final ListView list = (ListView) customView.findViewById(R.id.levels_list);
final CheckBox keep = (CheckBox) customView.findViewById(R.id.levels_keep);
String [] items = getResources().getStringArray(R.array.difficultyLevel_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, items);
list.setAdapter(adapter);
list.setItemChecked(level, true);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
level = position;
}
});
new AlertDialog.Builder(this)
.setView(customView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyLog.w(TAG,"Level is " + level);
if( keep.isChecked() ) {
Context context = PlayersActivity.this;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sp.edit();
editor.putString(context.getString(R.string.pref_level_key), String.valueOf(level));
editor.commit();
}
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
关于android - 将复选框添加到单选集 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15448828/