Jetpack Compose——Switch、CheckBox、RadioButton简单介绍

Switch

首先看看参数:

Switch(
    checked: Boolean,//是否选中
    onCheckedChange: ((Boolean) -> Unit)?,//点击回调
    modifier: Modifier = Modifier,//布局修饰
    enabled: Boolean = true,//是否可用
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },//事件交互
    colors: SwitchColors = SwitchDefaults.colors()//切换颜色
) 

使用:

        val checkState = remember {
            mutableStateOf(true)
        }
        Switch(
            checked = checkState.value,
            onCheckedChange = { checkState.value = it },
            colors = SwitchDefaults.colors(
                //多种颜色属性,设置滑块及背景颜色
                checkedThumbColor = Color.White,
                checkedTrackColor = Color.Green
            )
        )

效果如图:

Jetpack Compose——Switch、CheckBox、RadioButton简单介绍

 CheckBox

CheckBox的参数和Switch类似

Checkbox(
    checked: Boolean,//是否选中
    onCheckedChange: ((Boolean) -> Unit)?,/点击回调
    modifier: Modifier = Modifier,//布局修饰
    enabled: Boolean = true,//是否可用
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },//事件交互
    colors: CheckboxColors = CheckboxDefaults.colors()//切换颜色
) 

使用demo:

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

效果如图:

Jetpack Compose——Switch、CheckBox、RadioButton简单介绍

RadioButton

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

使用:

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

 效果如图:

Jetpack Compose——Switch、CheckBox、RadioButton简单介绍

这三个控件就介绍到这里吧

上一篇:centos7-docker安装


下一篇:2021全国职业院校技能大赛--基于Docker-compose编排部署sentiment-analyzer情感分析系统