2016-03-01 64 views
0

我想在我的應用程序中使用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

回答

0

試試這個

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"/> 
+0

感謝,你可以給我'@繪製/ android_button'代碼?謝謝你 –

+0

參考http://www.vankouteren.eu/blog/2011/09/custom-checkboxes-for-checkboxpreferences-on-android/ – theremin

0

您可以使用setTag()getTag()方法來解決此問題。 從您的xml文件中,將android:tag="integer"添加到CheckBoxPreference中,並使用getMethod在您的java文件中獲取該整數,然後使用setMethod並將其保存到共享首選項中。