如何在 Jetpack Compose 中为 TextField 设置 inputType

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

xml 中的等价物是 inputType

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Only numbers please!" />
stack overflow How to set the inputType for a TextField in Jetpack Compose
原文答案

答案:

作者头像

你可以使用类似的东西:

TextField(
        ....,
        keyboardOptions = 
             KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
        )
作者头像

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

            )