如何在preference.xml窗口中创建RadioButton组?
我是 Java Android 开发的初学者。我使用的是 Eclipse SDK 3.6.1 版本。我有一个首选项窗口,其中有两个复选框和一个后退按钮。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="PIN requirement">
<CheckBoxPreference
android:title="Use PIN"
android:defaultValue="true"
android:key="checkboxPref" />
<CheckBoxPreference
android:title="Don't use PIN"
android:defaultValue="false"
android:key="checkboxPref2" />
</PreferenceCategory>
<PreferenceCategory>
<Preference
android:title="Back"
android:key="customPref" />
</PreferenceCategory>
</PreferenceScreen>
如何将两个CheckBox更改为RadioButton组?
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I have a preferences window with two checkbox and one back button.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="PIN requirement">
<CheckBoxPreference
android:title="Use PIN"
android:defaultValue="true"
android:key="checkboxPref" />
<CheckBoxPreference
android:title="Don't use PIN"
android:defaultValue="false"
android:key="checkboxPref2" />
</PreferenceCategory>
<PreferenceCategory>
<Preference
android:title="Back"
android:key="customPref" />
</PreferenceCategory>
</PreferenceScreen>
How to change two CheckBox in to the RadioButton group?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(4)
试试这个:
try this:
如果您只需要启用或禁用 PIN,则在这种情况下只需一个 CheckBoxPreference 就足够了(请参阅下面的示例代码,第一类)。当您需要从设置列表(ListPreference)中选择某些内容时,通常会使用单选按钮 - 例如(参见示例代码,第二类)来选择颜色。
此示例的源代码为:
对于 ListPreference,您还需要一个 arrays.xml 文件,该文件位于“values”文件夹中:
另请参阅一些使用 PreferenceActivity 的很棒的示例 - 它们对我帮助很大:
Android 偏好设置;
如何创建一组RadioButtons而不是列表;< br>
如何在首选项摘要中显示 Android 首选项的当前值?
If you need just to enable or disable using PIN, only one CheckBoxPreference will be enough in this case (see example code below, First Category). RadioButtons are usually used, when you need to choose something from a list of settings (ListPreference) - for example (see example code, Second Category), to pick a color.
The source code for this example will be:
For ListPreference you will also need an arrays.xml file, which is located in the "values" folder:
See also some great examples, working with PreferenceActivity - they helped me a lot:
Android Preferences;
How to create a group of RadioButtons instead of a list;
How to display the current value of an Android Preference in the Preference summary?
在某些情况下,让首选项充当单选按钮是一个很好的功能,例如在每个选项下包含摘要文本的简单方法。
使一组复选框表现得像一组单选按钮相当简单。要在首选项中执行此操作,请将组中的所有复选框注册到同一个 OnPreferenceChangeListener,然后在此侦听器中使用 findPreference() 查找其他复选框并调用 setChecked(false);
唯一的缺点是复选框看起来不像单选按钮......
In some situations, having Preferences behave as radio buttons is a nice feature e.g a simple way of including summary text under each option.
Making a group of checkboxes behave like a group of radio buttons is fairly simple. To do this in Preferences, register all the checkboxes in the group to the same OnPreferenceChangeListener then in this listener use findPreference() to find the other checkboxes and call setChecked(false);
The only downside is that the checkboxes will not look like radio buttons...
我认为这非常简单,如果这就是您想要的:
在偏好 xml 中添加复选框,如下所示:
<前><代码>
<复选框首选项
android:key="prefkey_cbp_2"
android:summary="@string/prefkey_cbp_2"
android:title="@string/prefkey_cbp_2"
android:defaultValue="false" />
//ETC...
在偏好设置类 onCreate 中构建一个 CheckBoxPreference 数组或列表或任何您喜欢的内容,如下所示:
使您的首选项类实现 OnPreferenceClickListener 并将 OnPreferenceClickListener 设置为复选框,如下所示:
然后只需覆盖 onPreferenceClick 并处理任何点击。例如,如果您希望复选框像单选按钮一样执行(在同一个单选组中),这意味着一次仅检查一个复选框,请执行以下操作:
<前><代码>@Override
公共布尔onPreferenceClick(首选项arg0){
for (CheckBoxPreference cbp : cbp_list) {
if (!cbp.getKey().equals(arg0.getKey()) && cbp.isChecked()) {
cbp.setChecked(假);
}
}
返回假;
}
I think this is pretty simple if that is what you want:
Add the checkboxes at the preferenses xml like that:
At the preferences class onCreate build an CheckBoxPreference array or list or whatever you like, like that:
Make your preferences class implement OnPreferenceClickListener and set the OnPreferenceClickListener to the checkboxes like that :
Then just override the onPreferenceClick and handle any click. For example if you want checkboxes to perform like radio buttons (in the same radiogroup), meaning, only one checkbox at the time to be check, do something like that: