Is there a way to remove focus and the cursor from...
# compose
g
Is there a way to remove focus and the cursor from a textfield when the keyboard edit is done?
Copy code
@Composable
    override fun TextFieldWithHint(
        text: String,
        hint: @Composable() () -> Unit,
        onDone: (String) -> Unit,
        backgroundColor: Color,
        textColor : Color,
        fontSize: TextUnit,
        keyboardType: KeyboardType,
        visualTransformation: VisualTransformation,
        cursorColor : Color
    ) {
        val state = state { TextFieldValue(text) } //, textRange) }
        Surface(color = backgroundColor) { //}, border = Border(5.dp, Color.White)) {
            //Row {
            Box(
                modifier = Modifier.fillMaxSize(),
                padding = 10.dp,
                gravity = ContentGravity.CenterStart
            ) { //, padding = 15.dp) {
                Stack(Modifier.weight(1f)) {
                    TextField(
                        //modifier = Modifier.fillMaxSize(),
                        value = state.value,
                        textStyle = TextStyle (
                            color = textColor,
                            fontSize = fontSize
                        ),
                        //textColor = textColor,
                        keyboardType = keyboardType,
                        imeAction = ImeAction.Done,
                        onValueChange = { state.value = it },
                        onImeActionPerformed = {
                            if (it == ImeAction.Done) {
                                //todo remove hack when android fixes it
                                //On older phones the shift key shows up as 00 in the string
                                val char : Char = 0x00.toChar()
                                var str = state.value.text.replace("${char}", "")
                                onDone(str) //.text.substring(state.value.selection))
                                hideKeyboard()
                            }
                        },
                        cursorColor = cursorColor,
                        visualTransformation = visualTransformation
                    )
                    if (state.value.text.isEmpty()) hint()
                }
            }
        }
    }
a
We are introducing a new API 
FocusRequester
 ~finalized in the upcoming dev17 release (already available in snapshot dailies).   The current way to remove focus is to attach a new 
Focus
 modifier on some random non-text composable and then invoke a 
FocusRequester
 on it.  (We also have plans to introduce a simpler "clear the focus" API later.)
we are also working on a "chat client" sample app that includes this behavior
👍 2