5

我使用jetpack compose创建一个editText,我想显示一个像“android:hint”之前的提示,所以我尝试使用decorationBox,但是在我创建它之后输入不显示并且日志可以显示我的输入内容。这是我的代码,

val passState= remember { mutableStateOf(TextFieldValue("")) }
BasicTextField(
    decorationBox = {
        Text("password",color = loginGrayColor)
    },
    value = passState.value,
    onValueChange = { passState.value = it ; Log.d("password",it.text) },
    singleLine = true,
    maxLines = 1,
    textStyle = TextStyle(
        fontSize = 15.sp,
        color = loginInputTextColor
    ),
    modifier = Modifier
        .padding(start = 10.dp, top = 10.dp)
        .height(20.dp)
)
4

1 回答 1

11

您必须添加innerTextField. 就像是:decorationBox

var value by remember { mutableStateOf(TextFieldValue("")) }
BasicTextField(
    value = value,
    onValueChange = { value = it },
    decorationBox = { innerTextField ->
        Row(
            Modifier
                .background(Color.LightGray, RoundedCornerShape(percent = 30))
                .padding(16.dp)
        ) {

            if (value.text.isEmpty()) {
                Text("Label")
            }
            innerTextField()  //<-- Add this
        }
    },
)
于 2021-04-09T10:42:47.953 回答