1

I have textField composable and button composable. I want that clicking at the button would erase the text in the textField composable.

example:

var text by remember 
mutableStateOf(TextFieldValue(""))}

TextField(
  value = text,
  onValueChange = { newValue -> text = newValue },
                    modifier = Modifier
                        .padding(8.dp),
                    )

Button(
 onClick = { 
                //TODO: clean the text in textFiled
                      },
            modifier = Modifier
                .size(200.dp, 40.dp)
            
        ) {
            Text(text = "erase textField"
        }

thanks

  • This may help: https://stackoverflow.com/questions/67217106/access-textfield-value-from-another-composable-function-in-jetpack-compose – Mayur Gajra May 17 '21 at 16:03

2 Answers2

1
  • Create a mutableState as follows -> var textState by remember { mutableStateOf("") }
  • Create Textfield -> TextField(value = textState, onValueChange = { textState = it })
  • In the onClick of the button invoke the textState -> textState = ""
0

You can just simply reset the value of the text mutableState:

Button(onClick = { text = TextFieldValue("") })
Róbert Nagy
  • 6,720
  • 26
  • 45
  • but it is not what I want. It's question for change data between composables. not just clean the data, more changes. Anyway, I think I know the solution but the stackoverflow rules can't let me write it. – Shilo Gavra May 18 '21 at 17:42