* So, how is the automatic saving of preferences here? The answer is simple, that is in the specific implementation of the addPreferencesFromResource method! Instead of being the same as now, it will automatically save data according to your selection. Of course, if we do not inherit PreferenceActivity, then we have to manually There is a SharedPreferences getSharedPreferences(String name, int mode) method in the Activity class *, and weĬan also operate on preferences through it *. * So, can we not inherit PreferenceActivity? Of course, do you remember, in fact, * For example, you can get the preference manager through getPreferenceManager * Inheriting PreferenceActivity, we can easily operate preferences. We chose Shandong, and then the page will be automatically closed, and the value corresponding to Shandong has also been written into the backend xml file. There are mainly PreferenceActivity, ListPreference, EditTextPreference, CheckBoxPreference, RingtonePreference.īelow we briefly introduce the usage of ListPreference: In order to simplify application development related to preferences, android provides us with a series of APIs to help us. We can quickly save certain values into the xml file by using preferences, and then we can read these settings information and perform corresponding operations. Various restrictions and implementation mechanisms show that preference is very suitable for parameter setting functions. The xml file is stored in the data/data/package name of your application/shared_prefs folder. In fact, the data stored in preferences will eventually be saved in the form of xml file format, and it can only save data in some basic formats. This has to start with the realization of preferences in Android. Secondly, it can help us to store data conveniently! Why does this place have to emphasize the word convenience? The reason is that we can not use it at all, we have another N ways to achieve the same function! Its appearance is equivalent to providing us with a convenient tool, of course, this tool is not necessary. Import of all, we make it clear that preference is related to data storage. Create a file settings.xml under /res/menu folder and copy following content into it: On click on this item, we will display the Settings preference screen. This menu will have only one menuitem, Settings. First we need to create a menu which will be shown when User clicks menu button. Now we need to integrate it with our actual Activity. We have now created the basic setup for Preferences in our app. Android_Preferences_example Hello world! Settings MainActivity Send crash reports Helps to fix bugs Sync frequency Set the sync frequency Set username Set your username User Profile Update Settings Code language: HTML, XML ( xml ) Create a file arrays.xml (if not present already in your project) in /res/values folder and copy following content into it: The list we defined in above Preference settings file will be declared as an array in arrays.xml. In this example we let user to decide what should be the frequency for data sync. ListPreference as its name suggest allows user to select any one item from a given list.In this example we define a property to check weather to send crash report or not. CheckBoxPreference allows you to define a boolean value in preference screen.In this example username is editable property. EditTextPreference is used to define an editable text property.The setting page has 3 different components to specify user settings. We created two different Preference categories, One which shows Users Profile related settings and another Update related settings. Note how we defined the user interface of settings page in above xml file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |