2016-03-01 67 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并将其保存到共享首选项中。