如何在Android Jetpack Compose中更改OutlineTextField边框宽度?

5

我的代码:

OutlinedTextField(
     value = state.value,
     onValueChange = { state.value = it },
     modifier = Modifier.fillMaxWidth().padding(start = 30.dp, end = 30.dp),
     label = { Text(text = "Something", fontSize = 14.sp) },
     shape = RoundedCornerShape(12.dp),
)

我想增加边框宽度,以支持颜色focusedBorderColordisabledBorderColor

3个回答

7

OutlinedTextField 中,轮廓边框被定义为一个常量值。

private val IndicatorUnfocusedWidth = 1.dp
private val IndicatorFocusedWidth = 2.dp

没有直接的方法来覆盖这些值。

因此,如果您需要实现动态边框宽度,您必须创建完整的自定义TextField组件。

您可以复制并粘贴OutlinedTextField.ktTextFieldImpl.kt中的完整代码,并根据需要修改它们以创建自定义组件。


1
所以,我遇到了类似的问题,对于OutlinedTextField的定义条件UnfocusedBorderThickness = 1.dp我并不满意。

enter image description here

我尝试了几个选项,对我有效的解决方案是自定义的一个。我正在使用BasicTextFieldOutlinedTextFieldDefaults.DecorationBoxOutlinedTextFieldDefaults.ContainerBox
var searchText by rememberSaveable { mutableStateOf("") }
val interactionSource = remember { MutableInteractionSource() }

BasicTextField(
    value = searchText,
    singleLine = true,
    interactionSource = interactionSource,
    cursorBrush = SolidColor(Color.White),
    onValueChange = { newText -> searchText = newText }
) { innerTextField ->
    OutlinedTextFieldDefaults.DecorationBox(
        value = searchText,
        innerTextField = innerTextField,
        enabled = true,
        singleLine = true,
        interactionSource = interactionSource,
        visualTransformation = VisualTransformation.None,
        placeholder = {
            Text(
                text = stringResource(R.string.text),
            )
        },
        container = {
            OutlinedTextFieldDefaults.ContainerBox(
                enabled = true,
                isError = false,
                interactionSource = interactionSource,
                colors = OutlinedTextFieldDefaults.colors(),
                shape = RoundedCornerShape(Dimens.ActionButtonRadius),
                focusedBorderThickness = 5.dp,
                unfocusedBorderThickness = 5.dp
            )
        }
    )
}

0
你可以像这样更改OutlinedTextField的边框
    var hasFocus by remember { mutableStateOf(false) }

    OutlinedTextField(
        modifier = modifier
            .border(
                width = 1.dp,
                color = if (hasFocus) Color.Red else Color.Unspecified
            )
            .onFocusChanged { focusState -> hasFocus = focusState.hasFocus },
        colors = TextFieldDefaults.outlinedTextFieldColors(
            focusedBorderColor = Color.Unspecified,
            unfocusedBorderColor = Color.Unspecified
        )
    )

另一个解决方案是使用BasicTextField而不是OutlinedTextField

иҝҷ并没жңүзңҹжӯЈеӣһзӯ”е…ідәҺеңЁдҪҝз”ЁOutlinedTextFieldж—¶еўһеҠ иҫ№жЎҶе®ҪеәҰзҡ„й—®йўҳгҖӮ - Wahib Ul Haq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接