gpt4 book ai didi

android - 包含 TextFieldValue(text ='' , selection=TextRange(0, 0), composition=null) 的 MutableState 无法使用当前的 SaveableStateRegistry 保存

转载 作者:行者123 更新时间:2023-12-04 23:52:03 27 4
gpt4 key购买 nike

我正在尝试实现我的 TextField 只能接受 10 位数字的要求,并且我使用了“rememberSaveable”,这样当用户导航到另一个屏幕然后返回此屏幕时,在 TextField 中输入的数字不会丢失。
我使用了 rememberSaveable ,例如:
var 查询通过 rememberSaveable { mutableStateOf(TextFieldValue("")) }
然后我在我的 TextField 中使用它作为:

            val maxChar = 10

TextField(
value = query,
onValueChange = {
if (it.text.length <= maxChar) query = it
},
label = {
Text(
"Name as on PAN Card",
color = colorResource(id = R.color.bright_green),
fontSize = with(LocalDensity.current) { dimensionResource(id = R.dimen._12ssp).toSp() },
fontFamily = FontFamily(Font(R.font.poppins_regular)),
textAlign = TextAlign.Left

)
},
interactionSource = interactionSource,
keyboardOptions = KeyboardOptions(keyboardType =KeyboardType.Number),


textStyle = TextStyle(
textAlign = TextAlign.Start,
color = colorResource(id = R.color.bright_green),
fontFamily = FontFamily(Font(R.font.poppins_regular)),

fontSize = with(LocalDensity.current) { dimensionResource(id = R.dimen._12ssp).toSp() }
),
modifier = Modifier
.drawBehind {
val strokeWidth = indicatorWidth.value * density
val y = size.height - strokeWidth / 2
drawLine(
indicatorColor,
Offset(TextFieldPadding.toPx(), y),
Offset(size.width - TextFieldPadding.toPx(), y),
strokeWidth
)
}
.constrainAs(name) {
bottom.linkTo(glNameBottom)
start.linkTo(glLeft)
end.linkTo(glRight)
width = Dimension.fillToConstraints
}
.height((mHeight.value / 10.5).dp),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
cursorColor = colorResource(id = R.color.bright_green),
focusedIndicatorColor = Transparent,
unfocusedIndicatorColor = Transparent,
disabledIndicatorColor = Transparent
)
)
但是在使用上面的代码时,我遇到了异常:
java.lang.IllegalArgumentException: MutableState containing TextFieldValue(text='', selection=TextRange(0, 0), composition=null) cannot be saved using the current SaveableStateRegistry. The default implementation only supports types which can be stored inside the Bundle. Please consider implementing a custom Saver for this class and pass it as a stateSaver parameter to rememberSaveable().
我该如何解决这个问题?

最佳答案

正如 documentation 上的建议对于BasicTextField ,您可以简单地使用 TextFieldValue.Saver :

var query by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("")) }

关于android - 包含 TextFieldValue(text ='' , selection=TextRange(0, 0), composition=null) 的 MutableState 无法使用当前的 SaveableStateRegistry 保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68464612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com