簡體   English   中英

一個可組合的值如何改變其他的可組合值

[英]how can one composable change other composable value

我有可組合的 textField 和可組合的按鈕。 我希望單擊按鈕會刪除 textField 可組合中的文本。

例子:

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"
        }

謝謝

您可以簡單地重置文本mutableState的值:

Button(onClick = { text = TextFieldValue("") })
  • 如下創建一個 mutableState -> var textState by remember { mutableStateOf("") }
  • 創建 Textfield -> TextField(value = textState, onValueChange = { textState = it })
  • 在 onClick 的按鈕調用 textState -> textState = ""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM