簡體   English   中英

如何在 android jetpack compose 的文本字段中設置數字限制?

[英]How can I put the limit for numbers in textfield for android jetpack compose?

我在 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))
 
}

您可以像這樣在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))
 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM