将文本保留在后面的basictextfield中

发布于 2025-02-13 02:50:43 字数 869 浏览 1 评论 0 原文

我的JetPack组合功能中有一个Bastictextfield。当我单击(用户现在已经将一些文本输入到文本字段中)在一个按钮上,以导航到我的navhost中的另一个可复合的文本,从该新视图中,请单击我来自我带有Textfield的组合,Textfield是空的。我想保留用户在导航之前输入的文本,但我无法弄清楚如何。在这里看,但没有找到答案。

建议?

这是我的代码:

@Composable
fun SearchBar(
    modifier: Modifier = Modifier,
    onSearch: (String) -> Unit = {}
) {
    var text by remember {
        mutableStateOf("")
    }

    Box(modifier = modifier) {
        BasicTextField(
            value = text,
            onValueChange = {
                text = it
                onSearch(it)
            },
            maxLines = 1,
            singleLine = true,
            textStyle = TextStyle(color = Color.Black),
            modifier = Modifier
                .fillMaxWidth()
                .shadow(5.dp, CircleShape)
                .background(Color.White, CircleShape)
        )
    }
}

I have a BasicTextField in my jetpack compose function. When i click(user has input some text into the textfield by now) on a button to navigate to another composable in my NavHost, and from that new view click on back to the composable which i came from which has the textfield, the textfield is empty. I want to keep the text that the user typed in before navigating, but I can't figure it out how. Have looked here but found no answer.

Suggestions?

Here is my code:

@Composable
fun SearchBar(
    modifier: Modifier = Modifier,
    onSearch: (String) -> Unit = {}
) {
    var text by remember {
        mutableStateOf("")
    }

    Box(modifier = modifier) {
        BasicTextField(
            value = text,
            onValueChange = {
                text = it
                onSearch(it)
            },
            maxLines = 1,
            singleLine = true,
            textStyle = TextStyle(color = Color.Black),
            modifier = Modifier
                .fillMaxWidth()
                .shadow(5.dp, CircleShape)
                .background(Color.White, CircleShape)
        )
    }
}

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

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

发布评论

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

评论(1

牵强ㄟ 2025-02-20 02:50:44

记住通过

根据 Restore UI州指南,您可以用来替换>记住 with 记住可储存 要保存您的状态:

  • 配置更改
  • 过程死亡和恢复
  • 您的合成型> code> navhost back stack

以及任何何种您的 searchbar 可以从组成中删除然后重新添加的其他情况(例如,您在伴奏者的pager 或在 lazycolumn lazyrow )。

var text by rememberSaveable {
    mutableStateOf("")
}

remember saves the value over recomposition (i.e., when your state changes, your composable automatically recomposes with the new state).

As per the Restore UI State guide, you can replace remember with rememberSaveable to save your state across:

  • Configuration changes
  • Process death and recreation
  • Your composable being put on the NavHost back stack

As well as any other case where your SearchBar could be removed from composition and then re-added (such as if you were using in Accompanist's Pager or in a LazyColumn or LazyRow).

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