bastictextfield并不是焦点上的标题

发布于 2025-02-05 19:49:27 字数 1708 浏览 1 评论 0原文

嗨,这是我的Bastictextfield:

fun TextFieldRow(data: TextFieldData) {
    val interactionSource = remember { MutableInteractionSource() }
    val text by data.text.observeAsState("")
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 20.dp, end = 20.dp, top = 17.dp)
    ) {
        BasicTextField(
            value = text,
            onValueChange = {
                data.text.value = it
            },
            modifier = Modifier
                .padding(vertical = 6.dp)
                .fillMaxWidth()
                .indicatorLine(
                    enabled = true,
                    isError = false,
                    interactionSource = interactionSource,
                    colors = TextFieldDefaults.textFieldColors()
                )
        ) { innerTextField ->
            TextFieldDefaults.TextFieldDecorationBox(
                value = text,
                innerTextField = innerTextField,
                enabled = true,
                singleLine = true,
                visualTransformation = VisualTransformation.None,
                interactionSource = interactionSource,
                contentPadding = TextFieldDefaults.textFieldWithLabelPadding(start = 4.dp),
                label = { Text(text = data.label) }
            )
        }
    }
}

我在TextFieldDecoration框中设置了标签。当我输入一些字母(标题上升)时,它可以很好地工作,但是当我以焦点为重点时,标题留在中间。有帮助吗?

编辑 我想要这样的东西,是因为我想管理填充物而不是Textfield而不是Textfield。

Material io example

Hi this is my BasicTextField :

fun TextFieldRow(data: TextFieldData) {
    val interactionSource = remember { MutableInteractionSource() }
    val text by data.text.observeAsState("")
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 20.dp, end = 20.dp, top = 17.dp)
    ) {
        BasicTextField(
            value = text,
            onValueChange = {
                data.text.value = it
            },
            modifier = Modifier
                .padding(vertical = 6.dp)
                .fillMaxWidth()
                .indicatorLine(
                    enabled = true,
                    isError = false,
                    interactionSource = interactionSource,
                    colors = TextFieldDefaults.textFieldColors()
                )
        ) { innerTextField ->
            TextFieldDefaults.TextFieldDecorationBox(
                value = text,
                innerTextField = innerTextField,
                enabled = true,
                singleLine = true,
                visualTransformation = VisualTransformation.None,
                interactionSource = interactionSource,
                contentPadding = TextFieldDefaults.textFieldWithLabelPadding(start = 4.dp),
                label = { Text(text = data.label) }
            )
        }
    }
}

I set the label in the TextFieldDecorationBox. It work well when I type some letters (the title goes up), but when I just take focus, the title stay in the middle. Any help ?

EDIT
I would like something like that, the reason why I use BasicTextField instead of TextField is because I want to manage padding.

Material io example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-02-12 19:49:27

您可以更改Innertextfield;

BasicTextField(
  //
){ innerTextField ->
   Box{
      if(text.value.isEmpty())
         Text(
            text = "Title",
            color = Color.Black.copy(alpha = 0.5f)
         )
      innerTextField()
   }
}

You can change innerTextField like;

BasicTextField(
  //
){ innerTextField ->
   Box{
      if(text.value.isEmpty())
         Text(
            text = "Title",
            color = Color.Black.copy(alpha = 0.5f)
         )
      innerTextField()
   }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文