Android development-set the creation of the interface, android development interface
Preface:
Recently, I was busy working on projects. It was rare to have time to sort out the knowledge I learned in projects.
Before using this function, we will first introduce five data storage methods, such as file storage, SharePrefence, and SQL, which use ContentProvider to store data and network storage data, sharePrefence is a lightweight storage, mainly used to store app settings. The setting interface is used in this storage mode.
For more information, see the following reference blog.
Android
Data StorageFive methods-Use of CSDN blog:
In the past, when we created the interface, we used the xml layout + activity file. The setting interface is similar
Step 1: Create an xml folder
Switch to the Project mode and create an xml folder to put the layout file of the Setting interface.
Step 2: Compile the layout File
Create an xml file in the xml folder.
If the name is pref_xxx, the official naming rules are prefixed with pref.
Open the xml file and we can find that the layout file is the same as the layout file we previously wrote, with the design mode and text mode. here we can see seven options. I will only explain EditTextPreference and SwitichPreference.
For details, refer to this article.Android: Explains how to create a Google-styleSettingsActivity-Simplified book
Combining the two images above, I will explain the relevant attributes.
DefaultValue is the default value,
The key is similar to the id defined in the layout file. We can find the control through the id. Similarly, here we can also find the control through the key.
Summary is the line under the activated Member
Title is the title, that is, the activated Member
EditViewPreference is a control that encapsulates EditView. When we click it, a dialog box will pop up asking us to input data. When we input data, the data will be saved in SharePreference.
Similarly, a switch is a switch that stores true or false in SharePreference when clicked.
If we just want to get an option and click it to open a browser, the above controls are not suitable. What should we do? I don't know. You didn't notice that the outermost layer uses PreferenceScreen.
We can use PreferenceScreen in the following way, and then set a listener for it in java code to jump to the browser.
The key can be defined in sting. xml, or directly set
Step 3: Create Fragment. For apps lower than Android 3.0, We need to display the settings in the Activity and extend the PreferenceActivity class. This is an extension of the traditional Activity class, which displays the setting list based on the hierarchy of the Preference object. When the user changes the PreferenceActivity, The PreferenceActivity automatically retains the settings related to each Preference. PreferenceFragment should be used for Android 3.0 and later versions. Compared with the preceding Activity, Fragment provides a more flexible architecture for the Application regardless of the Activity to be built. Now, the lowest version is android4.0, so we can simply discard the previous method and use Fragment.
Create a new Fragment
Because we set the interface, we do not need to use it like normal fragment, so we will cancel all three options.
First, we need to modify the fragment so that it inherits the PreferenceFragment
Then, add a line of code in the onCreate method, and use the addPreferenceFromResource method to bind the xml
If fragment has been used before, we should know that we can only find the control in the onViewCreated method.
The key is used to find the control on the settings page.
Call the static method getdefasharsharedpreference to obtain the sharePreference, and then use the get method to obtain the previously saved values.
We need to mention that the original SharedPreference can save int boolean String and other types of data. However, if we use settings to save the data set by the app, sharedPreference has only two types of data: String and boolean. To use int, convert the obtained String to the int type.
After finding the control, we can call related methods to set the listener for it to implement related functions.
Step 4: dynamically add to framelayout
We need to create a new activity file, and its layout requires a framelayout
Then, we dynamically add and set the Fragment through the fragment method.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.setting_framlayout,settingFragment).commit();
After learning fragment, we should know that when we use Fragment, there are two packages for us to choose, one is v4 and the other is app, use the fragment of the v4 package for better compatibility (earlier version)
Therefore, it should be noted that the fragment used is the Fragment of the app package. At first, the v4 package I used had an error. After finding the cause, I found that the settingFragment inherited the PreferenceFragment, preferenceFragment inherits the Fragment under the app package.
Supplement:
You can use the setEnable method (the object is the control) to disable the control (this method can be used if a password lock is set)
You can convert a Preference to a related object. For example, a Preference can be converted to an EditTextPreference, or a SwitchPreference can be converted to a Preference, then you can call the method of the relevant object (same as the method of using the control defined in xml)