"Turn" Pro Android Learning Note (63): Preferences (7): Code Control Preferences

Source: Internet
Author: User

[-]

    1. Code Implementation Preference
    2. Save state with preference
    3. Dialogpreference
Code Implementation Preference

View can be set without XML, the code is set directly, and the preference preference is the same. The following is an example of the Code setting list preference, which is the same as the class that points to preferencefragment through the header, and the implementation in preferenceactivity.

public class Listpreferencefragment extends Preferencefragment {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Addpreferencesfromresource (r.xml.fightoptions);
//through key to get prefrenence
listpreference listpref = (listpreference) findpreference ("Selected_flight_sort_option");
Listpref. setentries (New string[]{"Entry 1", "Entry 2", "Entry 3"});
Listpref. setentryvalues (New string[]{"0", "1", "2"});
}
}

If you need to start with creating Preferencescreen, here's an example:

public class Listpreferencefragment extends Preferencefragment {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
//Create Preferencescreen
preferencescreen screen = Getpreferencemanager (). Createpreferencescreen (Getactivity ());
Create Preferencecategory, and add preference screen
Preferencecategory inlineprefcat = new Preferencecategory (getactivity ());

Inlineprefcat.settitle ("Hello");
screen.addpreference (INLINEPREFCAT);
Create a list preference and join the preference category
Listpreference listpref = new Listpreference (getactivity ());

Listpref.setkey ("My_list_pref");
Listpref.settitle ("Code for list preference");
Listpref.setsummary ("Test of preference written in full code");
Listpref.setentries (New string[]{"Entry 1", "Entry 2", "Entry 3"});
Listpref.setentryvalues (New string[]{"0", "1", "2"});
screen.addpreference (LISTPREF);
Add preference screen to preference fragment
Setpreferencescreen (screen);

}
}

Save state with preference

Preference information is stored in the device by file, and we can use this feature to store data and state, such as the highest score of the game. In the following example, a preference storage file is used to save two parameters, and "initialized" is a Boolean that indicates whether the application runs for the first time. "Call_number" is a counter, and each call to the Datastore () method adds one.

Private final String INITIALIZER = "initialized";
Private final String Callnum = "Call_number";

private void DataStore () {
Equivalent to Preferencemanager.getdefaultsharedpreferences (this), or by Getshearedpreferences (Name,mode) to specify the file name, Can be written by editor. In addition to Mode_private, there are mode_world_readable and mode_world_writeable, which are the permissions that are used to create the preference that are set. If we are restricted to use in this application, we can set mode_private.
sharedpreferences prefs = getpreferences (mode_private);
Boolean haspreferences = Prefs.getboolean (INITIALIZER, false);
int num = prefs.getint (callnum, 0);
if (haspreferences) {
LOG.D ("PRO", "We ' ve been called" + num + "times before.");
}else{
LOG.D ("PRO", "first time ever being called.");
}
//write preference
Editor editor = Prefs.edit ();
Editor.putboolean (INITIALIZER, true);
num + +;
editor.putint (callnum, num);
Editor.commit ();
or editor.apply ();
}

Dialogpreference

Dialogpreference is the base class of Edittextpreference and Listpreference, if we need to have our own frame effect, can inherit dialogpreference from custom layout, click Trigger Processing, And the write preference file is processed in Ondialogclose ().

The example code covered in this blog post can be downloaded in the Pro Android Learning: Preference (Preferences) Small example.

RELATED Links: My Android development related articles

"Go" Pro Android Learning Note (63): Preferences (7): Code Control Preferences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.