Android PreferenceActivity of

2010-12-30  来源:本站原创  分类:Mobile  人气:252 

Did not see a lot of the book are introduced PreferenceActivity do, and I just then used in the project, so put their own use in this summary, but also easy to find later.

PerferenceActivity what to see the screenshot below:

Android PreferenceActivity of

Android PreferenceActivity of

Android System screenshot (left) MusicPlayer Setting screenshot (right)

Well, we saw a large number of Android system itself uses PreferenceActivity come to the right configuration and management systems for information, it is how to save data in it, how to create PrefenceActivity of it, but the key is how to trigger the corresponding event yet.

Look at the first question, PreferenceActivity is how to save the data. Understanding of the system are aware Android, Android system has four basic data storage method, one SharedPreference, the second is the file and the third is SQLite, Fourth ContentProvider. Look out now, Preference, is to use SharedPreferneces on the form to save the key on, of course, we can also SharedPreferences to get PreferenceActivity setting.

The second question: how to create a PreferenceActivity. In fact, the creation of Eclipse to provide the corresponding tools, and create Layout is basically the same. As follows:

Create Android projects, and add an Android xml file. Note that this choice is not the Layout, but the Preference, and pay attention to Folder path is res / xml.

Android PreferenceActivity of

Add a complete, in res / xml / add preference.xml open the file. You can see Android also provides us with two edit modes, visual design and xml source code design. Recommended structure to create. As shown:

Android PreferenceActivity of

Here we look at PrefeneceActivity several elements which are provided for use. Click the Add button to open the new window you can see the following:

Android PreferenceActivity of

CheckBoxPreference: CheckBox selection, the corresponding value of the ture or flase. Figure:

Android PreferenceActivity of

EditTextPreference: Enter the edit box, is String type, the dialog box will pop up for input.

Android PreferenceActivity of

ListPreference: list selection, the pop-up dialog box for selection.

Android PreferenceActivity of

Preference: only the text display, need to be used in combination with the other.

Android PreferenceActivity of

PreferenceCategory: used for grouping. Results are as follows:

Android PreferenceActivity of

PreferenceScreen: PreferenceActivity the root element, it must have.

RingtonePreference: System-ling sound choice.

Android PreferenceActivity of

OK, Preferenc introduce the basic elements completed, the next section will use them to create a full Preference and display.

-------------------------------------------------- -----------------------------

Analysis MusicPlayer Setting, the first part of the "My Location", including "wireless cable" and "Use GPS" two parts, and are CheckBox, learning under the previous section, should include a PreferenceCategory and two CheckBoxPreference.

Xml Code:

<PreferenceCategory android:title=" My location
" android:key="set_local
">
        <CheckBoxPreference android:key="apply_wifi
"
                android:summary=" Use the wireless network in your application ( For example, Google map ) See the location
"
android:title=" Use the wireless network
" android:defaultValue="true">
        </CheckBoxPreference>
        <CheckBoxPreference android:key="apply_gps
"
                android:summary=" Navigate to the street level ( Need to consume more power as well as the weather allows )
"
android:title=" Use GPS
">
        </CheckBoxPreference>
</PreferenceCategory>

Of course, the above code can also be used to provide the IDE tool for Android directly generated. Video structured as follows:

Android PreferenceActivity of

PreferenceCategory Attribute:

title: title of show

key: unique identifier (at least in the same program only), SharedPreferences also will save the data value for this Key, you can also access the information stored key value (the same).

CheckBoxPreference Attribute:

Key: unique identifier.

title: display the title (large font)

summary: subtitle (small font)

defaultValue: default (of course, can only be true or false where a)

Preference.xml The second part of "the wireless and network settings", this section includes more content, but also a little complicated, step by step analysis.

xml code:

<
PreferenceCategory
 android
:title
=" Wireless and network settings "
>

        <
CheckBoxPreference
 android
:key
="apply_fly"

                android
:summary
=" Disable all wireless connections "
 android
:title
=" Flight mode "
>

        </
CheckBoxPreference
>

        <
CheckBoxPreference
 android
:key
="apply_internet"

                android
:summary
=" Disable shared via USB Internet Connection "
android
:title
="Internet Share "
>

        </
CheckBoxPreference
>

        <
CheckBoxPreference
 android
:key
="apply_wifi"

                android
:summary
=" Open Wi-Fi"
 android
:title
="Wi-Fi"
>

        </
CheckBoxPreference
>

        <
Preference
 android
:summary
=" Set up and manage your wireless access point "
 android
:title
="Wi-Fi Set "

                android
:dependency
="apply_wifi"
 android
:key
="wifi_setting"
>

        </
Preference
>

        <
CheckBoxPreference
 android
:key
="apply_bluetooth"

                android
:summary
=" Enable Bluetooth "
 android
:title
=" Bluetooth "
>

        </
CheckBoxPreference
>

        <
Preference
 android
:summary
=" Manage connections-device device name and the detectability of "

                android
:title
=" Bluetooth settings "
 android
:dependency
="apply_bluetooth"

                android
:key
="bluetooth_setting"
>

        </
Preference
>

        <
EditTextPreference
 android
:key
="number_edit"

                android
:title
=" Enter a phone number "
>

        </
EditTextPreference
>

        <
ListPreference
 android
:title
=" Sector settings "
 android
:entries
="@array/department"

                        android
:entryValues
="@array/department_value"
android
:dialogTitle
=" Select the sector "

                        android
:key
="depart_value"
>

        </
ListPreference
>

        <
RingtonePreference
 android
:ringtoneType
="all"
 android
:title
=" -Voice "
android
:showDefault
="true"
 android
:key
="ring_key"
android
:showSilent
="true"
>

        </
RingtonePreference
>

</
PreferenceCategory
>

Structure corresponding to Figure:

Android PreferenceActivity of

The second part of the first three are all CheckBoxPreference, not the heart to say, from <Preference android: key = "bluetooth_setting" /> Start.

Preference attribute analysis:

Key: unique identifier.

title: display the title (large font)

summary: subtitle (small font)

dependency: sub (Well meaning), which identifies a particular element of this element subsidiaries (usually CheckBoxPreference), dependency is the subsidiary element key. Preference elements in the above code attached to the key equal to "apply_bluetooth" the CheckPreference element is true, when CheckPreference, Preference was available, otherwise unavailable.

EditTextPreperence Attribute:

Key: unique identifier.

title: display the title (large font)

ListPreference Attribute:

Key: unique identifier.

title: display the title (large font)

dialogTitle: the title of the pop-up dialog box

entries: the values shown in the list. As an array, read through the resource file set.

entryValues: list the actual stored value, the corresponding entries. As an array, read through the resource file set. The following code shows the contents of the document arrays.xml:

<
resources
>

        <
string
-array
 name
="department"
>

                <
item
>
 General Department </
item
>

                <
item
>
 Administrative Department </
item
>

                <
item
>
 Foreign Trade Department </
item
>

        </
string
-array>

        <
string
-array
 name
="department_value"
>

                <
item
>
001</
item
>

                <
item
>
002</
item
>

                <
item
>
003</
item
>

        </
string
-array>

</
resources
>

RingtonePreference: Ling sound (temporarily not used before), being ignored.

OK, Preference.xml content has been completed analysis, attributes are roughly the same, that person will power to try the sort. So how to show the contents Preference.xml out?

Layout by continuing from the Activity class to be displayed, the previously mentioned, PreferenceActivity is devoted to show preference, so long as to create a class can inherit from PreferenceActivity. Code:

public class Setting extends PreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //  The value will be automatically saved to SharePreferences
                addPreferencesFromResource(R.xml.preference);
        }
}

The next step is running the program, showing the fruits of labor. So far, most of the work has been completed, all the values are saved to the SharedPreferences, we can also read to the saved results.

Another important issue is how to respond to PreferenceActivity operation. In fact, as long as the method of rewriting PreferenceActivity the onPreferenceTreeClick can, through the parameters to determine the preference is that one element, and according to the need for action.

@Override
public
 boolean
 onPreferenceTreeClick(PreferenceScreen preferenceScreen,
                Preference preference) {
        return
 false
;
}

Ok, this content on PreferenceActvity the like would be finished off, what the future will make up a significant discovery.

相关文章
  • Android PreferenceActivity study notes 2010-12-11

    In Ap sometimes need to set some configuration parameters, these parameters through the configuration file. To set these parameters need to provide a UI, for this demand, Android provides a preferenceActivity. PreferenceActivity by reading the pre-de

  • Android PreferenceActivity of 2010-12-30

    Did not see a lot of the book are introduced PreferenceActivity do, and I just then used in the project, so put their own use in this summary, but also easy to find later. PerferenceActivity what to see the screenshot below: Android System screenshot

  • Android之PreferenceActivity应用详解 2014-03-17

    为了引入这个概率 首先从需求说起 即:现有某Activity专门用于手机属性设置 那么应该如何做呢 为了引入这个概率 首先从需求说起 即:现有某Activity专门用于手机属性设置 那么应该如何做呢? 根据已学知识 很快一个念头闪过 即:Activity + Preference 组合 前者用于界面构建 后者用于设置数据存放 其实 这是正确的 但是 这会比较繁琐 因为 每个设置选项 都要建立与其对应的Preference 所以 现在有更好的选择了 那就是本文的主角:PreferenceActiv

  • PreferenceActivity Keys 2010-06-19

    PreferenceActivity To introduce the probability that the first start with the demand: an Activity dedicated to mobile phones existing property is set then how? According to soon have knowledge, came the thought that: Activity + Preference combined th

  • PreferenceActivity the use of android 2010-04-14

    Just learning the android in preferenceActivity use the following code fragment posted their own, we want to be useful. Oh, how long is the beginning of each, hoping that you get. android through the configuration file automatically generated Prefere

  • Android the PreferenceActivity (rpm) 2011-01-09

    Did not see a lot of the book are introduced PreferenceActivity do, and I just then used in the project, so put their own use in this summary, but also easy to find later. PerferenceActivity what to see the screenshot below: Android System screenshot

  • android's PreferenceActivity 2011-01-14

    Preface During this time of android platform open source projects - StandupTimer, which is designed by the jwood android a relatively simple application, used to control the meeting time, similar to the stopwatch count down. PreferenceActivity Prefer

  • Android之PreferenceActivity应用详解(2) 2014-06-07

    看到很多书中都没有对PreferenceActivity做介绍,而我正好又在项目中用到,所以就把自己的使用的在这总结一下,也方便日后查找 看到很多书中都没有对PreferenceActivity做介绍,而我正好又在项目中用到,所以就把自己的使用的在这总结一下,也方便日后查找. PerferenceActivity是什么,看下面的截图: Android系统截图(左)MusicPlayer Setting截图(右) 好了,我们看到Android系统本身就大量用到了PreferenceActivity

  • Android之PreferenceActivity 2015-01-18

    看到很多书中都没有对PreferenceActivity做介绍,而我正好又在项目中用到,所以就把自己的使用的在这总结一下,也方便日后查找. PerferenceActivity是什么,看下面的截图: Android系统截图(左) MusicPlayer Setting截图(右) 好了,我们看到Android系统本身就大量用到了PreferenceActivity来对系统进行信息配置和管理,那么它是怎么保存数据的呢,如何创建PrefenceActivity的呢,更关键是怎样触发相应事件的呢. 先看

  • The PreferenceActivity Android 2011-01-14

    http://www.cnblogs.com/wservices/archive/2010/07/08/1773449.html

  • 15 Android sorted out very useful code fragments (skills) ophone 2010-05-02

    1: see if there are memory card inserted String status = Environment.getExternalStorageState (); if (status.equals (Enviroment.MEDIA_MOUNTED)) ( Help with SD card inserted ) 2: Let a transparent Activity OnCreate is not set Layout this.setTheme (R.st

  • Sorted out the 15 Android useful code fragments (skills) ophone 2010-05-12

    <Transfer from sjl599 http://sjl599.javaeye.com/blog/657993> 1: see if there are memory card inserted String status = Environment.getExternalStorageState (); if (status.equals (Enviroment.MEDIA_MOUNTED)) ( Help with SD card inserted ) 2: Let a trans

  • Android useful snippets of code 2010-06-12

    1: see if there are memory card inserted String status = Environment.getExternalStorageState (); if (status.equals (Enviroment.MEDIA_MOUNTED)) ( Help with SD card inserted ) 2: Let a transparent Activity OnCreate is not set Layout this.setTheme (R.st

  • Android - SharedPreference Application 2010-06-30

    Note : Other applications call this parameter file : // Create a need to call the application to save the file parameters Context // The first parameter is there to save the file by the package name // The second argument that ignores the security le

  • Android / OPhone develop fully handouts 2010-07-08

    Android / OPhone develop fully handouts Basic Information Innovation: Press: China Water Power Press ISBN: 9787508475271 Added time :2010-7-1 Publication Date: June 2010 format: 16 Open Page: 466 More exciting information Address: http://www.china-pu

  • Android to use the code 2010-08-29

    1: see if there are memory card inserted String status = Environment.getExternalStorageState (); if (status.equals (Enviroment.MEDIA_MOUNTED)) ( Help with SD card inserted ) 2: Let a transparent Activity OnCreate is not set Layout this.setTheme (R.st

  • PreferenceActivity usage profile 2010-09-20

    In the development of the application process, we need to use a great opportunity to set the function parameters, then Android applications, how we set the parameters and parameter storage interface it, let's introduce Android in a special Activity-P

  • Android Preference Summa content changes in real-time display 2010-10-31

    In the Android's Preference, when the input in a EditTextPreference after the closure, and can not see the input in real time, which is sometimes inconvenient, this is a very popular demand. In fact, can be achieved by the following method. public cl

  • android useful code snippets 2010-11-27

    Directory • 1: see if there is a memory card inserted • 2: for a transparent Activity • 3: Set the handle of the screen element • 4: Send SMS • 5: Send MMS • 6: Send Mail • 7: sign up for a BroadcastReceiver • 8: Definition ContentObserver, listening

  • Android 20 served a useful code snippet 2010-12-15

    Part of the finishing part of the practice, I hope to feel the intention of this post is a good gossip landlord agree not to link the code! (Annex where the content is the code content, look no time to take care of the Friends of the Friends of the d