4

我在片段布局中有一个 Material Button Toggle Group(需要单选和选择),但是在加载我的应用程序时,我希望已经选择了一个按钮。我认为将其添加到我的 Main ActivityonCreate()就可以了(我的按钮的 id 是paletteA):

paletteA.isPressed = true

但相反,我的应用程序只是在加载时崩溃,并且“运行”窗口中没有显示任何错误。感觉有些东西没有及时加载或为空,即使我的所有片段都已加载等等,这是我的onCreate(). 有任何想法吗?

4

1 回答 1

6

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

要在默认情况下将要加载的特定按钮设置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">
于 2021-03-03T22:08:56.440 回答