尝试单击一个切换按钮并取消选中另一个切换按钮。 Android Studio-Kotlin

Try to click a togglebutton and make the other togglebutton unchecked. Android Studio-Kotlin

所以,大家好,在 android-studio 中,我有一个水平线性布局,还有两个并排的按钮,它们是切换按钮,我们称它们为 X 和 Y,我想做的是,一旦我单击 X,X 就会被选中,然后当我尝试单击 Y 时,Y 会被选中,但 X 会被取消选中,那么我该怎么做呢?我尝试这样做但最终没有成功。

    if (beginnerButton.isChecked) {
        balleButton.isChecked = false
    }
    else if (balleButton.isChecked) {
        beginnerButton. = false
    }

在 XML 中定义 OnClick,如下所示

<ToggleButton
        android:id="@+id/beginnerButton"
        android:layout_width="wrap_content"
        android:onClick="clickBeginnerButton"
        android:layout_height="wrap_content"
        />

    <ToggleButton
        android:id="@+id/balleButton"
        android:onClick="clickBalleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

在科特林中:

  public fun clickBeginnerButton(view: View) {
    balleButton.isChecked = false
  }

  public fun clickBalleButton(view: View) {
    beginnerButton.isChecked = false
  }

它将按您的预期工作

希望对您有所帮助