如何在 Jetpack Compose 中为 TextField 设置 inputType

时间:2021-03-04 20:23:09

标签: 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!" />

3 个答案:

答案 0 :(得分:6)

使用keyboardOptions

TextField(
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)

答案 1 :(得分:1)

您可以使用以下内容:

TextField(
        ....,
        keyboardOptions = 
             KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
        )

答案 2 :(得分:-1)

你喜欢这样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(),

            )