我想在我的应用程序中使用PreferenceScreen
来设置一些设置。我已经定制了此页面,并为任何偏好设置了布局。所有的偏好是好的,但CheckBoxPreferences
,当我点击它并没有显示任何反应,这是每时每刻。我可以设置是错误的!我为自定义CheckBox设置了android:id="@+android:id/checkbox"
。
复选框自定义XML:如何在Android上的PreferenceScreen中使用自定义CheckBox
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+android:id/title"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:textColor="#c0c0c0"
android:textSize="13sp" />
<CheckBox
android:id="@+android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="16dp"/>
</RelativeLayout>
PreferenceSreen XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:example="http://schemas.android.com/apk/res-auto"
xmlns:sample="http://schemas.android.com/apk/res-auto">
<CheckBoxPreference
android:defaultValue="true"
android:key="setting_main_subtitle_show"
android:layout="@layout/pref_checkbox_layout"
android:summary="Summary Text"
android:title="Title" />
</PreferenceScreen>
AppPreference java代码:
public class AppPreference {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private Context context;
private static final String KeyShowSubTitle = "setting_main_subtitle_show";
public AppPreference(Context context){
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
this.editor = sharedPreferences.edit();
this.context = context;
}
public Boolean getShowSubTitle(){
return sharedPreferences.getBoolean(KeyShowSubTitle, true);
}
}
的java的MainPage代码:
Boolean show_subTitle = appPreference.getShowSubTitle();
if (show_subTitle == true){
/// SubTitle Text
toolbar.setSubtitle(appPreference.getSubTitleText());
} else {
toolbar.setSubtitle("");
}
我怎样才能解决这个问题,并在PreferenceScreen
使用自定义CheckBox
?
感谢,你可以给我'@绘制/ android_button'代码?谢谢你 –
参考http://www.vankouteren.eu/blog/2011/09/custom-checkboxes-for-checkboxpreferences-on-android/ – theremin