Previous article Android Apidemo sample resolution: App->preferences->launching Preferences Advanced Preferences Advancedpreferences defined in the.
This article specifically describes Advancedpreferences, an example called advanced, because it involves custom preference and refreshes a preference value in a worker thread.
Preference for display in preferenceactivity (generally shown as a list) in a preference base class. Somewhat similar to a view displayed in the activity. Preference actually accesses an item that corresponds to a shared preferences, and the key defined by preference is the key value used to access the shared preferences.
As with view, you can customize view, and you can also customize preference in Android to show preferences for managing application customizations. This example mypreference a custom preference to store the number of times a user clicks on the preference, the type is an integer, and the initial value is defined as 100. Its corresponding definition in Advanced_preferences.xml is as follows:
<com.example.android.apis.app.mypreference
android:key= "My_preference"
android:title= "@string/title_my_preference"
android:summary= "@string/summary_my_preference"
Android:defaultvalue= "100″/>
Preference defines a number of attributes, such as Default Value, dependency, enabled, icon, key, and so on have a corresponding method to operate. and provides two Listener:preferencechangelistener, Preferenceclicklistener, allows the application to respond to preference value change events, or the user clicks on the preference event.
Here is a description of how to customize a preference in the order of Mypreference code.
1. Derived from the preference base class.
public class Mypreference extends
preference
2. Similar to Custom view you can customize layout for custom preference. Mypreference uses r.layout.preference_widget_mypreference, the definition is simple, only one textview, and its ID is mypreference_widget. You typically use Setwidgetlayoutresource to set up layout resources for preference derived classes in a constructor.
This is the constructor
called
by the Inflater public mypreference (context context, AttributeSet Attrs) {
s Uper (context, attrs);
Setwidgetlayoutresource (r.layout.preference_widget_mypreference);
}
3. If there is a need to set properties for the view in a custom layout, you can do so in the view view. Onbindview The following code sets the value to Mclickcounter for TextView.
@Override
protected void Onbindview (view view) {
Super.onbindview (view);
Set our custom inside the layout
final TextView mytextview
= (TextView) View.findviewbyid (r.id.myprefere Nce_widget);
if (Mytextview!= null) {
mytextview.settext (string.valueof (Mclickcounter));
}
}
4. If you define an initial value for the custom preference in XML, such as the mypreference initial value android:defaultvalue= "100″, we want to use this initial value in the code to initialize the variable mclickcounter. The Mclickcounter type is an integer that is used to hold the user's key number.
@Override
protected Object ongetdefaultvalue (TypedArray A, int index) {
//This preference type ' s value type ' Teger, so we read the
"default//value from" attributes as an Integer.
Return A.getinteger (index, 0);
}
@Override
protected void Onsetinitialvalue (Boolean restorevalue, Object defaultvalue) {
if (restorevalue) {
//Restore state
Mclickcounter = Getpersistedint (Mclickcounter);
} else {
//Set State
int Value = (Integer) defaultvalue;
Mclickcounter = value;
Persistint (value);
}
}