如何在 Jetpack Compose 中为 TextField 设置 inputType

Cri*_*tan 9 android android-jetpack-compose android-compose-textfield

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

xml 中的等效项是inputType

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Only numbers please!" />
Run Code Online (Sandbox Code Playgroud)

Cri*_*tan 14

使用keyboardOptions

TextField(
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
Run Code Online (Sandbox Code Playgroud)

  • 如果不需要 {,.-} (keyboardType = KeyboardType.NumberPassword) 可以使用 (5认同)

Liv*_*vin 7

你喜欢这个键盘选项

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(),

            )
Run Code Online (Sandbox Code Playgroud)