21

我想限制用户可以在 Jetpack Compose 的 TextField 中输入的内容。我怎么做?

xml中的等价物是inputType

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Only numbers please!" />
4

3 回答 3

39

使用键盘选项

TextField(
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
于 2021-03-04T20:23:09.210 回答
1

你可以使用类似的东西:

TextField(
        ....,
        keyboardOptions = 
             KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
        )
于 2021-03-05T09:02:48.073 回答
0

Ty 喜欢这个KeyboardOptions

var textShopName by remember { mutableStateOf("") } 

OutlinedTextField(
            keyboardOptions = KeyboardOptions(
                capitalization = KeyboardCapitalization.None,
                autoCorrect = true,
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Next
            ),
            value = textShopName,
            onValueChange = { textShopName = it },
            label = { Text("Shop Name") },
            modifier = Modifier
                .padding(start = 16.dp, end = 16.dp, top = 20.dp)
                .fillMaxWidth(),

            )
于 2021-07-19T16:38:46.160 回答