我重构了我的代码,从一个Activity(包含复选框)转移到Fragment。它曾经在Activity中按要求工作(双状态复选框)但现在由于某种原因复选框是一个三态复选框。
片段:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="kob.outdoor.ontheflyclient.activities.settings.SettingsFragment">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:id="@+id/lytSettingsTop"
>
<CheckBox
android:id="@+id/chkSettingsMonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:layout_marginLeft="20dp"/>
...
这不起作用(三态):
chkMonday.setChecked(settings.getBoolean(getString(R.string.chkSettingsMondayKey), true));
复选框有三态,我无法找出原因:
发现这有效(两州):
boolean monday = settings.getBoolean(getString(R.string.chkSettingsMondayKey), true);
chkMonday.setChecked(monday);
你有任何解释吗? 谢谢!