我在 android jetpack compose 中有一個文本欄位,所以我想限制數字,例如;用戶只能寫從 1 到 10 的數字,可以在 jetpack compose 中完成嗎?
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}
uj5u.com熱心網友回復:
您可以onValueChange像這樣為內部設定條件:
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
val maxNumbers = 10
OutlinedTextField(
value = text,
onValueChange = { if (it.toInt() <= maxNumbers) text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/494704.html
標籤:安卓 科特林 android-jetpack-compose
