【发布时间】:2020-06-08 03:54:30
【问题描述】:
长话短说。我正在单选组内创建单选按钮。我将根据用户的选择对我的表进行过滤。我默认选中了一个单选按钮。因此,当我尝试检查其他按钮时,它会保持选中状态。事实上,这两个按钮都被检查了。太奇怪了。也许这是因为我以编程方式做所有事情。设计仍在进行中。我只是停在这里,因为我不知道这是否是视觉错误。我正在使用 Kotlin 进行 Android 开发。
显示按钮:
private fun displayChoices(choices: List<FiltersList.Choice>, multipleChoice: Boolean) {
val radioGroup = RadioGroup(this)
for (choice in choices) {
val button = if (multipleChoice) {
CheckBox(this)
} else {
RadioButton(this)
}
button.apply {
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
setMargins(16, 8, 16, 8)
}
text = choice.display
isChecked = choice.selected
setTextColor(ResourcesCompat.getColor(resources, R.color.colorTextClicked, null))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
buttonTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, R.color.colorButton, null))
}
}
radioGroup.addView(button)
}
filters_content.addView(radioGroup)
}
我的布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/colorFilterBoxBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FilterDialogActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/filters_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
观点:
我以编程方式选择“任何日期”按钮并保持选中状态。我从服务器获取数据,默认需要选择哪些项目。
【问题讨论】:
-
你能用对话框 xml 更新你的答案吗?
-
我在 LinearLayout id 中渲染所有内容:filters_content。所有这些活动都是我的对话
标签: android kotlin radio-button radio-group