Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the inputType for a TextField in Jetpack Compose

I'd like to restrict what the user can type in a TextField in Jetpack Compose. How do I do that?

The equivalent in xml is inputType:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Only numbers please!" />
like image 983
Cristan Avatar asked Mar 04 '21 20:03

Cristan


3 Answers

Use KeyboardOptions:

TextField(
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
like image 77
Cristan Avatar answered Oct 14 '22 05:10

Cristan


You can use something like:

TextField(
        ....,
        keyboardOptions = 
             KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
        )
like image 26
Gabriele Mariotti Avatar answered Oct 14 '22 06:10

Gabriele Mariotti


Ty like this 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(),

            )
like image 1
Livin Avatar answered Oct 14 '22 05:10

Livin