Jetpack之RadionBtton、Switch、Checkbox组件介绍

本文介绍了JetpackCompose中的RadioButton,Switch,Checkbox组件的构造函数、参数以及使用方法,指出它们遵循MaterialDesign设计规范,且组件间参数相似,表明掌握Compose基础知识不难,预示着作者将深入探讨更多组件。
摘要由CSDN通过智能技术生成

Jetpack Compose系列(9) - RadionBtton、Switch、Checkbox组件介绍

RadioButton

跟View体系一样,Compose也有RadioButton单选按钮组件,遵从Material Design风格。

其构造函数如下:

@Composable
fun RadioButton(
    selected: Boolean,
    onClick: (() -> Unit)?,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    colors: RadioButtonColors = RadioButtonDefaults.colors()
)

各参数含义如下:

· selected: Boolean : 按钮是否选中。

· onClick: () -> Unit :点击RadioButton的回调。

· modifier: Modifier = Modifier : 其布局的修饰符,之前文章有详细解释,这里不做赘述

· enabled: Boolean = true: 表示RadioButton是否可用。如果为false,则此按钮将不可选择,并以禁用的UI状态显示。

· interactionState: InteractionState = remember { InteractionState() } :事件交互,自定义按钮显示的不同状态。

· colors: RadioButtonColors = RadioButtonDefaults.colors() :RadioButton在不同状态下的颜色。

例如如下代码:

val selectState = remember {
    mutableStateOf(true)
}
val notSelectState = remember {
    mutableStateOf(false)
}
Column() {
    RadioButton(
        selected = selectState.value,
        onClick = { selectState.value = !selectState.value },
        colors = RadioButtonDefaults.colors(
            selectedColor = Color.Blue,
            unselectedColor = Color.Gray
        )
    )
    RadioButton(
        selected = notSelectState.value,
        onClick = { notSelectState.value = !notSelectState.value },
        colors = RadioButtonDefaults.colors(
            selectedColor = Color.Blue,
            unselectedColor = Color.Gray
        )
    )
}

对应的显示效果为:

image.png

用惯了View体系的RadioButton的同学可能会发现Compose的RadioButton就是一个Button,不会像XML中的还有text属性。除此之外其余差别不大,在此不再赘述。

Switch

专门表示选中状态的组件还有一个,Switch,其构造函数如下:

fun Switch(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    colors: SwitchColors = SwitchDefaults.colors()
)

其参数含义与RadioButton差不多,例如如下代码:

val checkState = remember {
    mutableStateOf(true)
}
val notCheckState = remember {
    mutableStateOf(false)
}
Column() {
    Switch(
        checked = checkState.value,
        onCheckedChange = { checkState.value = it },
        colors = SwitchDefaults.colors(
            checkedThumbColor = Color.White,
            checkedTrackColor = Color.Red
        )
    )
    Switch(
        checked = notCheckState.value,
        onCheckedChange = { notCheckState.value = it },
        colors = SwitchDefaults.colors(
            checkedThumbColor = Color.White,
            checkedTrackColor = Color.Red
        )
    )
}

对应的显示效果为:

image.png

CheckBox

同样作为选择状态组件checkbox的构造函数参数跟Switch几乎差不多:

fun Checkbox(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    colors: CheckboxColors = CheckboxDefaults.colors()
)

各参数含义在此不做赘述。

val boxState = remember {
    mutableStateOf(true)
}
val notBoxtate = remember {
    mutableStateOf(false)
}
Column() {
    Checkbox(
        checked = boxState.value,
        onCheckedChange = { boxState.value = it },
        colors = CheckboxDefaults.colors(
            checkedColor = Color.Blue,
            uncheckedColor = Color.Gray
        )
    )
    Checkbox(
        checked = notBoxtate.value,
        onCheckedChange = { notBoxtate.value = it },
        colors = CheckboxDefaults.colors(
            checkedColor = Color.Blue,
            uncheckedColor = Color.Gray
        )
    )
}

对应的展示效果为:

image.png

最后,你会发现,这三个组件几乎是同样的参数和使用套路,这也从侧面说明掌握Jetpack Compose其实并不难。后续相关组件简单的我会一笔带过,开始Jetpack架构组件之旅。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值