Jetpack Compose学习 RadioButton

Jetpack Compose学习 -------- RadioButton 单选按钮

RadioButton 单选按钮 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

@Composable
fun RadioButtonSample() {
val radioOptions = listOf<String>("A", "B", "C")
val (selectedOption, onOptionSelected) = remember {
mutableStateOf(radioOptions[1])
}
Column {
radioOptions.forEach { text ->
Row(
Modifier
.fillMaxWidth()
.selectable(
selected = text == selectedOption,
onClick = {
onOptionSelected(text)
}
)
.padding(horizontal = 16.dp)
) {
RadioButton(
selected = (selectedOption == text),
onClick = {
onOptionSelected(text)
}
)
Text(
text = text,
style = MaterialTheme.typography.body1.merge(),
modifier = Modifier.padding(16.dp)
)

}
}
}
}