<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="CheckBoxPreference" >
<CheckBoxPreference
android:key="checkbox_0"
android:summary="这是一个勾选框A"
android:title="CheckBox_A" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
Second , MainActivity.java
package com.bo.preferencedemo;
import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
public class MainActivity extends PreferenceActivity {
Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.preference);
mContext = this;
//CheckBoxPreference组件
CheckBoxPreference mCheckbox0 = (CheckBoxPreference) findPreference("checkbox_0");
mCheckbox0.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
//这里可以监听到这个CheckBox 的点击事件
return true;
}
});
mCheckbox0.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference arg0, Object newValue) {
//这里可以监听到checkBox中值是否改变了
//并且可以拿到新改变的值
Toast.makeText(mContext, "checkBox_0改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show();
return true;
}
});
}
}
Third, questions , results are shown , how to modify the summary, title font size ?
------ Solution ---------------------------------------- ----
custom CheckBoxPreference
public class MyCheckboxPreference extends CheckBoxPreference {
public MyCheckboxPreference (Context context, AttributeSet attrs) {
super (context, attrs);
}
@ Override
protected void onBindView (View view) {
/ / TODO Auto-generated method stub
super.onBindView (view);
ViewGroup preferenceLayout = (ViewGroup) view;
TextView title = (TextView) ((ViewGroup) preferenceLayout.getChildAt (1)). getChildAt (0);
TextView summary = (TextView) ((ViewGroup) preferenceLayout.getChildAt (1)). getChildAt (1);
}
}
onBindView call timing is the onclick callback , so it is necessary to MyCheckboxPreference click event in an instance variable to write a mTitleSize global variable , until onBindView time , setTextSize (mTitleSize);
---- - Solution --------------------------------------------
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference"
android:layout="@layout/test"/>
</PreferenceCategory>
test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"/>
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"/>
</LinearLayout>
------ For reference only ----------------------------------- ----
tried or not ah ? What have you tried it ?
------ For reference only -------------------------------------- -
help experts to help ah ? Do not sink ah, grateful .
没有评论:
发表评论