[英]How to use custom CheckBox in PreferenceScreen on Android
我想在我的應用程序中使用PreferenceScreen
進行一些設置。 我已經定制了此頁面,並為任何首選項設置了布局。 所有首選項都可以,但是CheckBoxPreferences
,當我單擊它時不顯示任何反應,每次都如此。 我可以設置是錯誤的! 我已經將android:id="@+android:id/checkbox"
為自定義CheckBox。
CheckBox自定義xml:
<?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);
}
}
MainPage Java代碼:
Boolean show_subTitle = appPreference.getShowSubTitle();
if (show_subTitle == true){
/// SubTitle Text
toolbar.setSubtitle(appPreference.getSubTitleText());
} else {
toolbar.setSubtitle("");
}
如何解決此問題並在PreferenceScreen
使用自定義CheckBox
?
您可以使用setTag()
和getTag()
方法來解決此問題。 從您的xml文件中,將android:tag="integer"
到CheckBoxPreference,並使用getMethod在Java文件中獲取該整數,然后執行操作並使用setMethod,然后將其保存在共享首選項中。
嘗試這個
custom_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="true"
android:button="@drawable/android_button"/>
default.xml
<CheckBoxPreference
android:key="@string/Drop_Option"
android:title="Close after call drop"
android:defaultValue="true"
android:widgetLayout="@layout/custom_layout"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.