编程方式填充preferences与复选框 [英] Programatically populating preferences with checkboxes
问题描述
在我的设置页面我有一个preference它取的,我想显示为单个复选框preferences切换,能够设置列表。
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
支持preferences的生成列表(查看Wi-Fi设置页),但列表preference
只允许您选择从列表中。
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.
我一直在寻找如何编程生成preferences但只设法找到如何改变preferences那些已经在XML属性。
I've been searching for how to generate preferences programatically 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 your 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);
}
这篇关于编程方式填充preferences与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!