我们如何在jetpack compose中创建一个圆形复选框?

问题描述 投票:0回答:3

通常可以使用修饰符将不同的形状分配给可组合项,但在此可组合项中并未这样做。

我希望图中标记的部分是一个圆圈

你可以看我下面写的代码

@Composable
fun StandardCheckbox(
    text: String = "",
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
) {
    Row(
        Modifier.padding(horizontal = SpaceMedium)
    ) {
        Checkbox(
            modifier = Modifier
                .clip(CircleShape),
            checked = checked,
            onCheckedChange = onCheckedChange,
            enabled = true,
            colors = CheckboxDefaults.colors(
                checkedColor = MaterialTheme.colors.primary,
                checkmarkColor = MaterialTheme.colors.onPrimary,
                uncheckedColor = MaterialTheme.colors.onBackground.copy(alpha = 0.3f)
            )
        )
        Spacer(modifier = Modifier.width(SpaceSmall))
        Text(
            text = text,
            color = MaterialTheme.colors.primary,
            modifier = Modifier.clickable {
                if (onCheckedChange != null) {
                    onCheckedChange(!checked)
                }
            }
        )
    }
}
android-jetpack-compose android-jetpack android-checkbox
3个回答
10
投票

为了实现具有原生体验的圆形复选框,并保留主体颜色和点击波纹效果,并保持简单,IconButton是最佳选择。


@Composable
fun CircleCheckbox(selected: Boolean, enabled: Boolean = true, onChecked: () -> Unit) {

    val color = MaterialTheme.colorScheme
    val imageVector = if (selected) Icons.Filled.CheckCircle else Icons.Outlined.Circle
    val tint = if (selected) color.primary.copy(alpha = 0.8f) else color.white.copy(alpha = 0.8f)
    val background = if (selected) color.white else Color.Transparent

    IconButton(onClick = { onChecked() },
        modifier = Modifier.offset(x = 4.dp, y = 4.dp),
        enabled = enabled) {
        
        Icon(imageVector = imageVector, tint = tint,
            modifier = Modifier.background(background, shape = CircleShape),
            contentDescription = "checkbox")
    }
}



7
投票

下面的代码来自

CheckboxImpl
composable

Canvas(modifier.wrapContentSize(Alignment.Center).requiredSize(CheckboxSize)) {
    val strokeWidthPx = floor(StrokeWidth.toPx())
    drawBox(
        boxColor = boxColor,
        borderColor = borderColor,
        radius = RadiusSize.toPx(),
        strokeWidth = strokeWidthPx
    )
    drawCheck(
        checkColor = checkColor,
        checkFraction = checkDrawFraction,
        crossCenterGravitation = checkCenterGravitationShiftFraction,
        strokeWidthPx = strokeWidthPx,
        drawingCache = checkCache
    )
}

drawBox
将始终绘制圆角矩形。无法定制。

要实现圆形复选框,您需要编写自定义

Composable
并绘制圆形而不是矩形。您可以使用
RadioButton
CheckboxImpl
可组合项作为参考。


0
投票
@Composable
fun CircularCheckbox(
    modifier: Modifier = Modifier,
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit
) {
    Box(
        modifier = modifier
            .size(24.dp)
            .background(
                color = if (checked) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
                shape = CircleShape
            )
            .clickable { onCheckedChange(!checked) },
        contentAlignment = Alignment.Center
    ) {
        if (checked) {
            Icon(
                imageVector = Icons.Default.Check,
                contentDescription = null,
                tint = Color.White,
                modifier = Modifier.size(16.dp)
            )
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.