Android/Kotlin Material 按钮组默认选择

Android/Kotlin Material Button Group default selection

我在 Fragment 布局中有一个 Material 按钮切换组(需要单选和选择),但是在加载我的应用程序时,我想要其中一个按钮 already被选中。我认为将其添加到我的 Main Activity onCreate() 中就可以解决问题(我按钮的 ID 是 paletteA):

paletteA.isPressed = true

但是我的应用程序只是在加载时崩溃并且 运行 window 中没有显示任何错误。感觉有些东西没有及时加载或者是空的,即使我所有的片段都已经加载等等,这是我 onCreate() 中的最后一件事。有什么想法吗?

MaterialButtonToggleGroup 小部件有一个名为 app:checkedButton 的 XML 属性,可用于加载选中的组中的按钮。

要将 MaterialButtonToggleGroup 中的特定按钮设置为默认加载,请将要检查的按钮的 ID 作为值传递给 app:checkedButton 属性。

<com.google.android.material.button.MaterialButtonToggleGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:singleSelection="true"
        android:id="@+id/activity_main_togglebutton"
        app:checkedButton="@id/activity_main_button2">