1.PreferenceActivity 介绍
android开发中一共为我们提供了4个组件,分别是CheckBoxPreference组件、EditTextPreference组件、 ListPreference组件、RingtonePreference组件
下面分别介绍
1.) CheckBoxPreference组件
CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中。
2.) EditTextPreference组件EditTextPreference 点击后会弹出一个输入框,输入的内容会以字符串的的形式储存在SharedPreferences中。
3.) ListPreference组件在res/array中先写两个数组,一个用与list的显示内容,一个用户list的选中数值。
ListPreference点击后会弹出一个列表框,选中后会将选中的内容(上面数组中的值)会以字符串的的形式储存在SharedPreferences中。4.) RingtonePreference组件
RingtonePreference点击后会弹出一个系统铃声的列表框,选中后会将选中的内容(uri字符集)会以字符串的的形式储存在SharedPreferences中。
2.在PreferenceActivity中加入自定义的layout布局文件
1.) 在layout下创建一个show_Button.xml布局文件
格式如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="9"/>
<Button
android:id="@+id/btn_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="显示"
android:layout_weight="1"/>
</LinearLayout>
2.) 在onCreate() 中获取Button
addPreferencesFromResource(R.xml.list);
setContentView(R.layout.show_Button);
注意 setContentView(R.layout.show_Button);要在addPreferencesFromResource(R.xml.list);下面
这样就可以使用findViewById() 来获取Button的焦点 并且绑定事件 不会报NullPointerException异常了
3.PreferenceScreen的使用
PreferenceScreen点击后可以弹出新的包含Preference元素的页面
<PreferenceScreen
android:key=""
android:title=""
android:persistent="false">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage=""
android:targetClass="" />
</PreferenceScreen>