本文介绍了使用复选框以编程方式填充首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的设置页面中,我有一个首选项,该偏好会获取一个可切换设置的列表,我希望将这些列表显示为各个复选框的首选项.

In my setting page I have a preference which fetches a list of toggle-able settings that I wanted to display as individual checkbox preferences.

我知道preferences.xml支持生成首选项列表(查看wi-fi设置页面),但是ListPreference仅允许您从列表中选择一个.

I know that preferences.xml supports generating lists of preferences (looking at wi-fi settings page) but ListPreference only allows you to select one from the list.

我一直在寻找如何以编程方式生成首选项,但仅设法找到如何更改XML中已经存在的首选项的属性.

I've been searching for how to generate preferences programmatically but have only managed to find how to change attributes of preferences that are already in the XML.

推荐答案

下面是一个简短的示例(假设您正在扩展PreferenceActivity):

Here is a short example (assuming you are extending PreferenceActivity):

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);

    PreferenceCategory category = new PreferenceCategory(this);
    category.setTitle("category name");

    screen.addPreference(category);

    CheckBoxPreference checkBoxPref = new CheckBoxPreference(this);
    checkBoxPref.setTitle("title");
    checkBoxPref.setSummary("summary");
    checkBoxPref.setChecked(true);

    category.addPreference(checkBoxPref);
    setPreferenceScreen(screen);
}

这篇关于使用复选框以编程方式填充首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:21