将文本保留在后面的basictextfield中
我的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)
)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
记住
通过根据 Restore UI州指南,您可以用来替换>记住 with
记住可储存
要保存您的状态:以及任何何种您的
searchbar
可以从组成中删除然后重新添加的其他情况(例如,您在伴奏者的pager 或在lazycolumn
或lazyrow
)。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
withrememberSaveable
to save your state across:NavHost
back stackAs 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 aLazyColumn
orLazyRow
).