android基础知识07:SharedPreferences和PreferenceActivity02—自定义

在博客《android基础知识07:SharedPreferences和PreferenceActivity》中我们介绍了SharedPreferences和PreferenceActivity的基本原理。本文我们将自顶一个activity来实现PreferenceActivity的功能。
通常我们看到设置界面都是一个屏的,一般这都是根据PreferenceActivity来实现了。但是如果出现一种情况,我的设置项很多,在一个列表中显示不完怎么办?理论上我们可以用分页来解决,但是这个到目前我还没有看到解决方案。另外一种,就是这里需要介绍的分频的方式。

在这里,你点击下方四个按钮,则会进入对应的设置项。
对于这种界面,有两种可行的实现方法:
方法1:使用多个Preference.xml文件,点击不同的按钮,则inflate不同的文件。这里存在一个问题,你必须制作4个最底下都是4个相同按钮的布局文件,然后根据点击不同的按钮进行切换布局。
方法2:使用一个布局,在使用时,隐藏或者显示不同的部分。比如下布局文件所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/pref_container_ll" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00000000"> <LayoutScrollView android:id="@+id/pref_default" android:layout_xml="@layout/android_settings_function" android:visibility="visible" style="@style/Layout_FF" /> <LayoutScrollView android:id="@+id/pref_function" android:layout_xml="@layout/android_settings_function1" android:visibility="gone" style="@style/Layout_FF" /> <LayoutScrollView android:id="@+id/pref_design" android:layout_xml="@layout/android_settings_function2" android:visibility="gone" style="@style/Layout_FF" android:background="#00000000" /> <LayoutScrollView android:id="@+id/pref_team" android:layout_xml="@layout/android_settings_function3" android:visibility="gone" style="@style/Layout_FF" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:background="#7c7c7b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dip" android:paddingBottom="3dip" android:paddingLeft="5dip" android:paddingRight="5dip"> <Button android:id="@+id/pref_setting_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="setting" /> <Button android:id="@+id/pref_function_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="function" /> <Button android:id="@+id/pref_design_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="design" /> <Button android:id="@+id/pref_team_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="team" /> </LinearLayout> </LinearLayout> 使用此布局时,点击不同的按钮则显示不同的LayoutScrollView,以此来完成我们分屏的目的。
这时,据会产生一个问题,我们在使用PreferenceActivity布局文件的那些元素时,并没有可以做到这一点的,怎么办呢?简单,自己写。自己写一个PreferenceView类,同时通过该类去实现属于你自己的CheckBoxPreferenceView等等元素。
之类我们介绍一下PreferenceView的实现,其布局文件preference.xml为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:ellipsize="marquee" android:fadingEdge="horizontal" android:text="TITLE" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:maxLines="2" android:text="description" /> </LinearLayout> 其代码为:
public abstract class PreferenceView extends LinearLayout implements OnClickListener, PreferenceInterface { protected String mKey; protected String mStringValue; protected boolean mBooleanValue; protected TextView mTitleView; protected TextView mSummaryView; protected Context mContext; public PreferenceView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; LayoutParams param = generateDefaultLayoutParams(); setClickable(true); setFocusable(true); setEnabled(true); setLayoutParams(param); setOrientation(HORIZONTAL); // setPadding(0, 0, 0, 13); setOnClickListener(this); LayoutInflater.from(context).inflate(R.layout.preference, this); } public void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); if (gainFocus) { onFocus(); } else { onUnFocus(); } // setPadding(0, 0, 0, 13); } protected void onFocus() { setBackgroundResource(android.R.drawable.list_selector_background); } protected void onUnFocus() { setBackgroundColor(0x00000000); } public String getTitle() { return mTitleView.getText().toString(); } public void onClick(View v) { onFocus(); } protected void save() { SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(mContext); SharedPreferences.Editor edit = sp.edit(); save(edit); edit.commit(); } }其中的PreferenceInterface只有两个函数:
public void load(SharedPreferences sp);
public void save(SharedPreferences.Editor edit);
这里大家可以对照一下系统自带的Preference的元素,就知道这个类的大概意思了。
于是对于一个CheckBoxPreferenceView ,我们该如何定义呢?
其布局文件定义如下:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="4dip" android:layout_gravity="center_vertical" android:focusable="true" android:clickable="true" /> 其代码为: public class CheckBoxPreferenceView extends PreferenceView implements OnCheckedChangeListener { protected CheckBox mCheckBox; private boolean mInit = false; public CheckBoxPreferenceView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate( R.layout.simeji_preference_checkbox, this); mCheckBox = (CheckBox) getChildAt(getChildCount() - 1); //mCheckBox.setChecked(mBooleanValue); mCheckBox.setOnCheckedChangeListener(this); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (mInit) { save(); } } @Override public void onClick(View v) { super.onClick(v); mCheckBox.setChecked(!mCheckBox.isChecked()); //save(); } @Override public void save(SharedPreferences.Editor edit) { edit.putBoolean(mKey, mCheckBox.isChecked()); } @Override public void load(SharedPreferences sp) { mInit = false; boolean flag = sp.getBoolean(mKey, mBooleanValue); mCheckBox.setChecked(flag); mInit = true; } }

于是,你就可以像使用系统自带的CheckBoxPreference 使用这个东西了。另外,你还可以据此自己定义ListPreference或者其他控件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值