Fragment中复选框的奇怪行为

时间:2017-12-03 18:44:05

标签: android android-fragments checkbox

我重构了我的代码,从一个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));

复选框有三态,我无法找出原因:

  • 加载Fragment时的默认状态:为空,突出显示边框。
  • 点击一次:取消选中(灰色)
  • 单击一下:选中(勾选标记+突出显示的边框)

发现这有效(两州):

boolean monday = settings.getBoolean(getString(R.string.chkSettingsMondayKey), true);
chkMonday.setChecked(monday);

你有任何解释吗? 谢谢!

0 个答案:

没有答案
相关问题