【问题标题】:How to create a TextField with underbar, without any background or border?如何创建一个带下划线的TextField,没有任何背景或边框?
【发布时间】:2021-07-31 01:30:30
【问题描述】:

我正在尝试创建一个 TextField,在 Jetpack 中使用下划线但没有任何其他边框或背景。我该怎么做?

这是我目前使用的代码:

val query = remember {mutableStateOf("")}
        TextField(
        value = query.value,
            onValueChange = { newValue -> query.value = newValue },
            label={Text("Dummy", color = colorResource(id = R.color.fade_green))},
            textStyle = TextStyle(
                textAlign = TextAlign.Start,
                color = colorResource(id = R.color.fade_green),
                fontFamily = FontFamily(Font(R.font.poppins_regular)),
                fontSize = 14.sp,
            ),
            modifier = Modifier
                .padding(start = 30.dp).border(0.dp, Color.Red),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.Transparent
            )
            )

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack-compose-text


    【解决方案1】:

    使用1.0.0,您可以使用:

    var text by remember { mutableStateOf("") }
    
    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        label = { Text("label") },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = Color.Transparent,
            //Color of indicator = underbar
            focusedIndicatorColor = ....,
            unfocusedIndicatorColor = ....,
            disabledIndicatorColor = ....
        )
    )
    

    如果要更改indicatorWidth,目前没有内置参数。

    您可以使用.drawBehind 修饰符来绘制一条线。比如:

    val interactionSource = remember { MutableInteractionSource() }
    val isFocused by interactionSource.collectIsFocusedAsState()
    
    val indicatorColor = if (isFocused) Color.Red else Color.Gray
    val indicatorWidth = 4.dp
    
    TextField(
        value = text,
        onValueChange = { 
           text = it },
        label={Text("Label")},
        interactionSource = interactionSource,
        modifier = Modifier
            .drawBehind {
                val strokeWidth = indicatorWidth.value * density
                val y = size.height - strokeWidth / 2
                drawLine(
                    indicatorColor,
                    Offset(0f, y),
                    Offset(size.width, y),
                    strokeWidth
                )
        },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = Color.Transparent,
            focusedIndicatorColor =  Transparent,
            unfocusedIndicatorColor = Transparent,
            disabledIndicatorColor = Transparent
        )
    )
    

    【讨论】:

    • 非常感谢。我们如何改变下划线的颜色?
    • @SparshDutta 只需使用focusedIndicatorColorunfocusedIndicatorColordisabledIndicatorColor。我已经更新了答案。
    • ,有没有办法改变下边栏的宽度?
    • 喜欢让它变薄还是变厚?
    • @SparshDutta 我已经更新了答案。目前没有indicatorWidth参数。
    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2015-08-22
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多