【发布时间】:2019-11-26 12:20:38
【问题描述】:
我有一个有趣的问题。 在我的代码中,我动态创建了一些 RadioButtons 并将其添加到 RadioGroup。但我无法理解如何使用 mvvm 和数据绑定但不使用 xml 以编程方式向每个 RadioButtons 添加可观察的布尔值... 因此,例如,如果我们在 xml 静态中有 RadioButtons,我们就会有类似的东西:
<RadioButton
android:id="@+id/someRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{viewModel.isChecked}"
android:text="@string/some_string" />
和
ObservableBoolean isChecked = new ObservableBoolean(false);
在我们的代码中。 但是,如果我们在 xml 中没有按钮但在代码中添加它,我们如何添加 observable 字段呢?喜欢
RadioButton radioButton = new RadioButton(this)
radioButton.text = localeLanguage.languageName
RadioGroup.LayoutParams params = RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.MATCH_PARENT)
radioButton.layoutParams = params
return radioButton
【问题讨论】:
标签: android mvvm observable android-databinding