Andoird Preference

本文深入解析了Android中PreferenceScreen和PreferenceCategory的使用,包括如何加载preference.xml文件,自定义Preference的步骤,以及如何通过监听器获取Preference数据。同时,文章提供了自定义Preference的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PreferenceScreen 代表显示一整个屏幕,内部嵌套PreferenceCategory标签,表示偏好类别,在PreferenceCategory标签内部可以随便存放复选框,输入框,列表等显示控件.可包含的控件内容在android.preference包下可查阅.xml文件编写好后,需要加载到activity中,对于偏好显示的xml加载,可以使用PreferenceActivity中的addPreferencesFromResource(),所以Activity需要继承PreferenceActivity
最新的API 提供的是PreferenceFragment 来加载preference.xml文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="爱好">
        <CheckBoxPreference
            android:key="checkbox_1"
            android:title="爱好1"
            android:summary="测试1"
            />
        <CheckBoxPreference
            android:key="checkout_2"
            android:title="爱好2"
            android:summary="测试2"
            />

    </PreferenceCategory>
</PreferenceScreen>

在PreferenceActivity中重写onPreferenceTreeClick()方法实现对该页面下所有preference 监听,也可以

preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        Log.e("sunming",preference.getKey()+" ");
        return false;
    }
});
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return false;
    }
});

实现对某个控件单独监听

获取preference的数据

 SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this) ;
System.out.println(new Boolean(prefs.getBoolean("checkbox",false)).toString());//false表示没有查到checkbox这个key的返回值

自定义Preference:这俩主要是重写

onCreateView
onBindView

在onCreateView中创建普通的布局,在onBindView 中获取View 控件 ,之后就是普通的处理逻辑,在obBindView 中获取控件,然后设置监听等操作

attr.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <declare-styleable name="PreferenceWithTip">
4         <attr name="tipstring" format="string"></attr>
5         <attr name="titlestring" format="string"></attr>
6     </declare-styleable>
7 </resources>

设计自定义Preference的布局 preferencewithtip.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal"
 6     android:paddingLeft="8dp"
 7     android:paddingRight="15dp"
 8     android:paddingTop="20dp"
 9     android:paddingBottom="20dp">
10     <TextView 
11         android:id="@+id/prefs_title"
12         android:layout_width="0dp"
13         android:layout_height="wrap_content"
14         android:layout_gravity="left"
15         android:gravity="left|center_vertical"
16         android:textSize="18sp"
17         android:layout_weight="1"/>
18     <TextView 
19         android:id="@+id/prefs_tip"
20         android:layout_width="0dp"
21         android:layout_height="wrap_content"
22         android:layout_gravity="right"
23         android:gravity="right|center_vertical"
24         android:textSize="18sp"
25         android:layout_weight="1"/>
26 
27 </LinearLayout>
继承Preference,实现自己的Preference类 PreferenceWithTip
1 public class PreferenceWithTip extends Preference {
 2     private static final String TAG = "PreferenceWithTip";
 3     String pTitle = null;
 4     String tipstring = null;
 5     
 6     @SuppressLint("Recycle")
 7     public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {
 8         super(context, attrs, defStyle);
 9         // 获取自定义参数
10         Log.i(TAG,"PreferenceWithTip invoked");
11         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);
12         tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);
13         pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);
14         ta.recycle();
15     }
16 
17     public PreferenceWithTip(Context context, AttributeSet attrs) {
18         this(context, attrs, 0);
19     }
20 
21     @Override
22     protected void onBindView(View view) {
23         super.onBindView(view);
24         TextView pTitleView = (TextView)view.findViewById(R.id.prefs_title);
25         pTitleView.setText(pTitle);
26         TextView pTipView = (TextView)view.findViewById(R.id.prefs_tip);
27         pTipView.setText(tipstring);
28     }
29 
30     @Override
31     protected View onCreateView(ViewGroup parent) {
32         return LayoutInflater.from(getContext()).inflate(R.layout.preferencewithtip,
33                 parent, false);
34     }
35     
36     //如需更新、保存数据则需要继续编写
37     
38 }
1             <com.ict.customview.PreferenceWithTip
2                 preference:tipstring=">"
3                 preference:titlestring="自定义测试" >
4                 <intent
5                     android:action="android.intent.action.VIEW"
6                     android:data="http://www.baidu.com" />
7             </com.ict.customview.PreferenceWithTip>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值