A brief introduction and usage of PreferenceFragment in Android
This article mainly introduces "the brief introduction and usage of PreferenceFragment in Android". In the daily operation, I believe that many people have doubts about the simple introduction and use of PreferenceFragment in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "simple introduction and use of PreferenceFragment in Android". Next, please follow the editor to study!
Catalogue
Introduction to PreferenceFragment
PreferenceFragment usage
PreferenceFragment extension
Introduction to PreferenceFragment
When we write a project, we basically have an option setting interface, the principle of this kind of setting interface is basically some local personalized settings, by reading local settings to change some differences in display (such as font size, theme color, WIFI automatic download, etc.). These settings are generally saved using Preference. Android provides a convenient base class PreferenceActivity for this kind of Activity (if it is Fragment, use PreferenceFragment, now it is recommended to use PreferenceFragmentCompat under the v7 package). These classes encapsulate Preference, which will help us to read and write settings automatically, making it convenient for developers to complete such functions.
PreferenceFragment usage
PreferenceFragment is located under the android.preference package, and now it is recommended to use the v7 package. This is still used here to illustrate the principle is the same.
Let's use it first:
Create a xml directory under the res directory
Create a xml file in the xml directory (the file name can be defined by yourself)
The file demonstrated here is pref_setting.xml.
Inherit PreferenceFragment and call the addPreferencesFromResource method in the onCreate method to load the resources under the xml directory.
Public class PreferenceTestFragment extends PreferenceFragment {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); addPreferencesFromResource (R.xml.pref_setting); / / load xml file}}
Running effect:
Controls used in pref_setting.xml can be viewed under the android.preference package, where there are several commonly used properties:
The attribute name uses the android:key storage key, which is the keyandroid:title title android:defaultValue default value when SharedPreferences is stored
Set click event
FindPreference ("setting_no_img") .setOnPreferenceClickListener (new Preference.OnPreferenceClickListener () {@ Override public boolean onPreferenceClick (Preference preference) {/ / todo return true;}})
The key in findPreference is the key declared in xml.
You can use registerOnSharedPreferenceChangeListener to listen for SharedPreferences value changes.
GetPreferenceScreen (). GetSharedPreferences (). RegisterOnSharedPreferenceChangeListener (this); PreferenceFragment extension
Custom controls can be implemented by inheriting Preference
Public class ThemePreference extends Preference {private CircleView circleImageView; public ThemePreference (Context context, AttributeSet attrs) {super (context, attrs); / / load layout file setWidgetLayoutResource (R.layout.item_theme_preference_preview);} / / bind view @ Override protected void onBindView (View view) {super.onBindView (view); int color = CommonSettingUtil.getInstance (). GetThemeColor () CircleImageView = (CircleView) view.findViewById (R.id.iv_preview); circleImageView.setBackgroundColor (color);} / * * refresh color display * / public void updateColor () {circleImageView.setBackgroundColor (CommonSettingUtil.getInstance (). GetThemeColor ());} at this point, the study on "brief introduction and use of PreferenceFragment in Android" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!