https://kotlinlang.org logo
#compose
Title
# compose
f

Fudge

07/16/2020, 1:35 PM
Is there some way to make a
TextField
unfocus? I can't draw focus away not by tapping outside, or by tapping the IME action on the keyboard, and I can't find a way to do it in code (something like a FocusNode I get from the
TextField
that I can call
unfocus()
on) I've found some outdated discussion about it: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1584069025456900?thread_ts=1584053267.453300&cid=CJLTWPH7S
👍 1
s

Siyamed

07/16/2020, 10:38 PM
@Ralston Da Silva I forgot how we do it now
m

Madhava

07/16/2020, 10:46 PM
+1 to this, im also keen to know how to get get focus off a textfield or hide the keyboard from an ime action.
r

Ralston Da Silva

07/16/2020, 11:30 PM
Sorry, we haven't added an API to do that yet. I filed a bug to make sure I add it: https://issuetracker.google.com/issues/161487952
For now, here is a workaround:
Copy code
@Composable
fun Sample() {
    var text by savedInstanceState { "" }
    val hostView = ViewAmbient.current
    Row(modifier = Modifier.tapGestureFilter { hostView.clearFocus() }) {
        TextField(value = text,
            onValueChange = { text = it },
            label = { Text("Enter Text Here") }
        )
    }
}
3 Views