如何在使用 MVVM 架构时使用 kotlin 代码检查单选按钮?

How to check radiobuttons using kotlin code while using MVVM architecture?

我在我的应用程序中使用 MVVM 架构,并且在单选组内的 2*2 网格中有 4 个单选按钮,但问题是 oncheckedChanged 没有在视图模型中被调用 class,这里是 xml代码:

          <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp"
            android:checkedButton="@id/singleRadioBtn"
            android:onCheckedChanged="@{viweModel::selectOption}"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton

                    android:layout_width="0dp"
                    android:layout_height="@dimen/_30sdp"
                    android:layout_weight="1"
                  ></RadioButton>

                <Space
                    android:layout_width="@dimen/_8sdp"
                    android:layout_height="match_parent" />

                <RadioButton

                    android:layout_width="0dp"
                    android:layout_height="@dimen/_30sdp"
                    android:layout_weight="1"
                   ></RadioButton>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_8sdp"
                android:orientation="horizontal">

                <RadioButton

                    android:layout_width="0dp"
                    android:layout_height="@dimen/_30sdp"
                    android:layout_weight="1"
               ></RadioButton>

                <Space
                    android:layout_width="@dimen/_8sdp"
                    android:layout_height="match_parent" />

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="@dimen/_30sdp"
                    android:layout_weight="1"
                  ></RadioButton>
            </LinearLayout>
        </RadioGroup>

在视图模型中:

 fun selectOption(radioGroup: RadioGroup, id: Int)
    {
        radioGroup.check(id)
    }

但是上面的函数没有被调用。那么我在这里做错了什么?请帮忙!

我认为您的问题不在于 DataBinding,而是因为您将 LinearLayouts 放在了 RadioGroup 中。由于 Radiogroup 是 LinearLayout 本身的子类,因此它会造成混乱,因此通常您必须在代码中做一些额外的事情才能使其工作(但在这种情况下 DataBinding 不是很有用),您可以通过讨论查看 here.

如果您尝试将 RadioButtons 放在 RadioGroup 中(没有嵌套的 LinearLayouts),我想您的方法应该被调用。

作为解决方法,您可以绑定另一个回调 - 每个 RadioButton 的 onClick(明确设置 RadioGroup 的索引):

//...........
<RadioButton
      android:layout_width="0dp"
      android:layout_height="30dp"
      android:layout_weight="1"
      android:onClick="@{(v) -> viewModel.selectOption(1)}"
></RadioButton>

<Space
     android:layout_width="8dp"
     android:layout_height="match_parent" />

<RadioButton

     android:layout_width="0dp"
     android:layout_height="30dp"
     android:layout_weight="1"
     android:onClick="@{(v) -> viewModel.selectOption(2)}"
></RadioButton>
//...............

更新

选择之一 - 使其根据您当前的模型工作:

  1. 给 Radiogroup 赋予 id(以便它可以用于绑定表达式)
  2. 为 RadioButtons 提供 ID
  3. 在 ViewModel 中稍微改变一下方法

XML

<RadioGroup
    android:layout_width="match_parent"
    android:id="@+id/radioGroup"
    .......>
<RadioButton
    android:layout_width="0dp"
    android:id="@+id/button1"
    android:onClick="@{(v) -> viewModel.selectOption(radioGroup,v)}" // <- v - is your button

视图模型

fun selectOption(radioGroup: RadioGroup, radioButton: View)
{
    radioGroup.check(radioButton.id)
}