Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

android - Programmatically populating preferences with checkboxes

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...