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

fae0ux8s  于 2023-08-01  发布在  Android
关注(0)|答案(3)|浏览(215)

我的编码:

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

tkclm6bt

tkclm6bt1#

轮廓边框定义为OutlinedTextField中的常量值。

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

字符串
没有直接的方法可以覆写这些值。
因此,如果需要实现动态边框宽度,则必须创建完整的自定义TextField Composables。
您可以将完整的代码复制粘贴到OutlinedTextField.ktTextFieldImpl.kt中,并根据需要对其进行修改,以创建自定义的Composables。

6pp0gazn

6pp0gazn2#

您可以像这样更改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

slwdgvem

slwdgvem3#

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


的数据
我尝试了几种选择,对我有效的解决方案是自定义的。我正在使用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
            )
        }
    )
}

字符串

相关问题