I started working with Jetpack compose (1.0.0-beta07) and I ran into a pretty strange problem with TextField. According to all possible documentation and instructions, I do everything right, but Android Studio constantly writes me the message None of the following functions can be called with the arguments supplied.
for TextField
Below is my written code, where Studio still underlines Text (label)
and text = it
, but I take it that it has a problem defining TextField
. The problem disappears when I replace remember {mutableStateOf ("text")}
with "text"
, but TextField
does not change the text when typing the keyboard.
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.input.KeyboardType
@Composable
fun SimpleTextField(label: String = "Label", key: String = "unknown", keyboardType: KeyboardType = KeyboardType.Text){
var text = remember {
mutableStateOf("text")
}
TextField(
value = text,
onValueChange = {
text = it
},
label = { Text(label) },
keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)
}
To read value entered in TextField in Android Compose, declare a variable, and assign this variable the value in the TextField whenever there is a change in the value. The same process holds for reading value entered in OutlineTextField composable.
Jetpack compose is a modern tool kit building native UIs for Android using Kotlin. Directly in jetpack compose does not support recycler view like the android app.
A Scaffold is a layout which implements the basic material design layout structure. You can add things like a TopBar, BottomBar, FAB or a Drawer.
Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose. But if you're modifying an existing app, you might not want to fully migrate your app all at once.
You can use:
var text = remember { mutableStateOf("text") }
TextField(
value = text.value,
onValueChange = {
text.value = it
},
label = { Text(label) },
keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)
or:
var text by remember { mutableStateOf("text") }
TextField(
value = text,
onValueChange = {
text = it
},
label = { Text(label) },
keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)
You can read more info about the delegated properties in the official doc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With