其他分享
首页 > 其他分享> > Jetpack Compose——Switch、CheckBox、RadioButton简单介绍

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
            )
        )

效果如图:

 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
            )
        )

效果如图:

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
            )
        )

 效果如图:

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

标签:Compose,remember,Jetpack,colors,value,RadioButton,Boolean,Modifier,true
来源: https://blog.csdn.net/lplj717/article/details/121948719