PreferenceActivity中添加自定义布局文件需要注意的地方

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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值