我试图实现我的 TextField 只能接受 10 位数字的要求,并且我使用了“rememberSaveable”,以便当用户导航到另一个屏幕然后返回到这个屏幕时,在 TextField 中输入的数字不是输了。
我使用过 rememberSaveable ,例如:
通过rememberSaveable { mutableStateOf(TextFieldValue("")) 进行var 查询}
然后我在我的 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().
我该如何解决这个问题?
答案 0 :(得分:0)
这是因为rememberSaveable
不支持TextFieldValue
的存储。根据文档,您可以创建自定义保护程序对象,但更好的解决方案是将您的值简单地存储在视图模型中。这样,它将在整个配置更改和导航过程中被记住。另外,我不认为 rememberSaveable
提供跨导航的状态保存,因此当您返回屏幕时,文本无论如何都会丢失(您可以通过将初始化更改为 var query by rememberSaveable { mutableStateOf("") }
来尝试)>
只需创建一个视图模型
class VM: ViewModel(){
var query by mutableStateOf(TextFieldValue(""))
private set
fun onQueryChange(newQuery: textFieldCount){
if (newQuery.text.length <= maxChar) query = newQuery
}
}
是的,应该这样做
在你的课堂上,
val maxChar = 10
TextField(
value = viewmodel.query,
onValueChange = viewmodel::onQueryChange,
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
)
)
答案 1 :(得分:0)
您需要将 mutableStateOf("")
与 rememberSaveable 一起使用,如下所示:
var query by rememberSaveable { mutableStateOf("") }
代替
var query by rememberSaveable { mutableStateOf(TextFieldValue("")) }