唯客微博客

专注于计算机,嵌入式领域的技术

0%

Jetpack Compose组件-单选按钮(RadioButton)

Jetpack Compose组件-单选按钮(RadioButton)

单选按钮(RadioButton)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Composable
fun RadioButton(
// 是否选中
selected: Boolean,
// 点击回调
onClick: (() -> Unit)?,
// 修饰符
modifier: Modifier = Modifier,
// 单选按钮使能
enabled: Boolean = true,
// 单选按钮的状态源
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
// 单选按钮的各种颜色
colors: RadioButtonColors = RadioButtonDefaults.colors()
): Unit
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
val radioOptions = listOf("Calls", "Missed", "Friends")
val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
// Note that Modifier.selectableGroup() is essential to ensure correct accessibility behavior
Column(Modifier.selectableGroup()) {
radioOptions.forEach { text ->
Row(
Modifier
.fillMaxWidth()
.height(56.dp)
.selectable(
selected = (text == selectedOption),
onClick = { onOptionSelected(text) },
role = Role.RadioButton
)
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = (text == selectedOption),
onClick = null // null recommended for accessibility with screenreaders
)
Text(
text = text,
style = MaterialTheme.typography.body1.merge(),
modifier = Modifier.padding(start = 16.dp)
)
}
}
}

三态复选按钮(TriStateCheckbox)

TriStateCheckbox具有选中/未选中/不确定三种状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Composable
fun TriStateCheckbox(
// 选中状态
state: ToggleableState,
// 点击回调
onClick: (() -> Unit)?,
// 修饰符
modifier: Modifier = Modifier,
// 复选按钮使能
enabled: Boolean = true,
// 复选按钮的状态源
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
// 复选按钮的各种颜色
colors: CheckboxColors = CheckboxDefaults.colors()
): Unit
-------------本文结束感谢您的阅读-------------

本文标题:Jetpack Compose组件-单选按钮(RadioButton)

文章作者:Vinx

发布时间:2023年01月11日 - 17:00

最后更新:2023年09月18日 - 11:39

原始链接:https://blog.vinkvin.com/post/58/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。