android 如何使用jetpack合成设置复选框的边框颜色

7kqas0il  于 2022-11-03  发布在  Android
关注(0)|答案(3)|浏览(169)

我需要实现如下所示复选框

我已经尝试探索了compose提供的所有方面,以改变如下复选框

colors: CheckboxColors = CheckboxDefaults.colors()

我找到了一些替代解决方案here,但是
1.对我不起作用
1.我觉得只是改变复选框边框的颜色不应该有这么多的工作左右的权利。

:可使用颜色字段更改复选标记颜色

colors = CheckboxDefaults.colors(checkmarkColor = Black)

任何帮助都是感激不尽的

f45qwnt8

f45qwnt81#

您需要实现CheckboxColors接口并创建您的配色方案。

@Stable
class YourCheckboxColors(
    private val checkedCheckmarkColor: Color,
    private val uncheckedCheckmarkColor: Color,
    private val checkedBoxColor: Color,
    private val uncheckedBoxColor: Color,
    private val disabledCheckedBoxColor: Color,
    private val disabledUncheckedBoxColor: Color,
    private val disabledIndeterminateBoxColor: Color,
    private val checkedBorderColor: Color,
    private val uncheckedBorderColor: Color,
    private val disabledBorderColor: Color,
    private val disabledIndeterminateBorderColor: Color
) : CheckboxColors {

    private val BoxInDuration = 50
    private val BoxOutDuration = 100
    private val CheckAnimationDuration = 100

    private val CheckboxRippleRadius = 24.dp
    private val CheckboxDefaultPadding = 2.dp
    private val CheckboxSize = 20.dp
    private val StrokeWidth = 2.dp
    private val RadiusSize = 2.dp

    @Composable
    override fun checkmarkColor(state: ToggleableState): State<Color> {
        val target = if (state == ToggleableState.Off) {
            uncheckedCheckmarkColor
        } else {
            checkedCheckmarkColor
        }

        val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
        return animateColorAsState(target, tween(durationMillis = duration))
    }

    @Composable
    override fun boxColor(enabled: Boolean, state: ToggleableState): State<Color> {
        val target = if (enabled) {
            when (state) {
                ToggleableState.On, ToggleableState.Indeterminate -> checkedBoxColor
                ToggleableState.Off -> uncheckedBoxColor
            }
        } else {
            when (state) {
                ToggleableState.On -> disabledCheckedBoxColor
                ToggleableState.Indeterminate -> disabledIndeterminateBoxColor
                ToggleableState.Off -> disabledUncheckedBoxColor
            }
        }

        // If not enabled 'snap' to the disabled state, as there should be no animations between
        // enabled / disabled.
        return if (enabled) {
            val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
            animateColorAsState(target, tween(durationMillis = duration))
        } else {
            rememberUpdatedState(target)
        }
    }

    @Composable
    override fun borderColor(enabled: Boolean, state: ToggleableState): State<Color> {
        val target = if (enabled) {
            when (state) {
                ToggleableState.On, ToggleableState.Indeterminate -> checkedBorderColor
                ToggleableState.Off -> uncheckedBorderColor
            }
        } else {
            when (state) {
                ToggleableState.Indeterminate -> disabledIndeterminateBorderColor
                ToggleableState.On, ToggleableState.Off -> disabledBorderColor
            }
        }

        // If not enabled 'snap' to the disabled state, as there should be no animations between
        // enabled / disabled.
        return if (enabled) {
            val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
            animateColorAsState(target, tween(durationMillis = duration))
        } else {
            rememberUpdatedState(target)
        }
    }
}

然后,您只需通过以下方式设置颜色:

Checkbox(
            colors = YourCheckboxColors(
                checkedBorderColor = Ink900,
                checkedBoxColor = Light,
                checkedCheckmarkColor = Ink900,
                uncheckedCheckmarkColor = Ink900.copy(alpha = 0f),
                uncheckedBoxColor = Light.copy(alpha = 0f),
                disabledCheckedBoxColor = Light,
                disabledUncheckedBoxColor = Light.copy(alpha = 0f),
                disabledIndeterminateBoxColor = Light,
                uncheckedBorderColor = Ink900,
                disabledBorderColor = Ink900,
                disabledIndeterminateBorderColor = Ink900,
            ),
            onCheckedChange = {}
        )
dsekswqp

dsekswqp2#

有默认颜色函数CheckboxDefaults.colors()。你可以改变具体的颜色。

@Composable
fun myCheckBoxColors(): CheckboxColors {
    return CheckboxDefaults.colors(
        checkedColor = Color.red,
        uncheckedColor = Color.transparent
    )
}
yzckvree

yzckvree3#

您可以尝试以下代码并使用Box自定义复选框边框。

@Composable
fun CustomCheckBox(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit),
checkBoxSize: Dp = 60.dp,
checkBoxBorderWidth: Dp = 1.dp,
checkBoxBorderColorSelected: Int = R.color.black,
checkBoxBorderColorUnSelected: Int = R.color.teal_200,
checkBoxCheckedIconColor: Int = R.color.black,
) {
// state is used to hold the checkbox click or not by default is false
val checkBoxState = remember { mutableStateOf(checked) }
// Ui for checkbox
Box(
    modifier = Modifier
        .border(BorderStroke(checkBoxBorderWidth,
            if (checkBoxState.value) colorResource(id = checkBoxBorderColorSelected)
            else colorResource(id = checkBoxBorderColorUnSelected)))
        .size(checkBoxSize)
        .background(Color.Transparent)
        .clickable {
            onCheckedChange(!checkBoxState.value)
            checkBoxState.value = !checkBoxState.value
        },
    contentAlignment = Alignment.Center
) {
    if (checkBoxState.value)
        Icon(Icons.Default.Check,
            tint = colorResource(id = checkBoxCheckedIconColor),
            contentDescription = "Custom CheckBox")
}
}

相关问题