【问题标题】:How to clear textfield value in Jetpack Compose?如何清除 Jetpack Compose 中的文本字段值?
【发布时间】:2022-05-07 17:10:41
【问题描述】:

我开发了一个带有尾随图标的可组合文本输入,我想在单击图标时清除文本输入。如何访问 textInput 值,以便清除它?

    @Composable
fun TextInput(
    myVal: String,
    label: String,
    placeholder: String="",
    helperText: String="",
    errorText: String="",
    onValueChange : (String) -> Unit){
    val hasError = !errorText.isNullOrEmpty()
    val helperColor =  if (hasError)
        Color(0xFFfe392f)
        else
            Color(0xFF5c6a79)

    Row() {
            Column() {
                TextField(
                    colors = TextFieldDefaults.textFieldColors(
                        backgroundColor = Color.Transparent,
                        textColor = Color(0xFF011e41),
                        cursorColor = Color(0xFF011e41),
                        focusedLabelColor = Color(0xFF011e41),
                        unfocusedLabelColor = Color(0xFF011e41),
                        unfocusedIndicatorColor = Color(0xFFebeced),
                        focusedIndicatorColor = Color(0xFF011e41),
                        errorCursorColor = Color(0xFFfe392f),
                        errorIndicatorColor = Color(0xFFfe392f),
                        errorLabelColor = Color(0xFFfe392f)
                    ),
                    value = myVal,
                    onValueChange = onValueChange,
                    label = { Text(label) },
                    placeholder = { Text(placeholder) },
                    isError = hasError,
                    trailingIcon = {Icon(Icons.Filled.Email, contentDescription = "sdsd", modifier = Modifier.offset(x= 10.dp).clickable {
                       //What should I do here?
                    })}
                )

                Text(
                    modifier = Modifier.padding(8.dp),
                    text = if (hasError) errorText else helperText,
                    fontSize = 12.sp,
                    color = helperColor,
                )
            }
    }
}

它是这样使用的:

var text by remember { mutableStateOf("") }
                    TextInput(myVal = text, label = "label", helperText = "", errorText = "my error") {text = it}

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack-compose-text


    【解决方案1】:

    您可以将 trailingIcon 属性与自定义 clickable 修饰符一起使用。
    比如:

    var text by rememberSaveable { mutableStateOf("") }
    
    TextField(
        value = text,
        onValueChange = { text = it },
        trailingIcon = {
            Icon(Icons.Default.Clear,
                contentDescription = "clear text",
                modifier = Modifier
                    .clickable {
                        text = ""
                    }
            )
        }
    )
    

    如果您使用的是TextFieldValue

    val content = "content"
    var text by rememberSaveable(stateSaver = TextFieldValue.Saver) {
        mutableStateOf(TextFieldValue(content))
    }
    
    TextField(
        value = text,
        onValueChange = { text = it },
        trailingIcon = {
            Icon(Icons.Default.Clear,
                contentDescription = "clear text",
                modifier = Modifier
                    .clickable {
                        text = TextFieldValue("")
                    }
            )
        }
    )
    

    【讨论】:

    • 这仅适用于文本变量引用字符串的情况。如果它是一个 TextFieldValue 对象,这将不起作用。 TextFieldValue 用于需要操作光标的情况,例如将光标放在文本末尾。
    • @Johann 在这种情况下,您只需在 trailingIconclickable 修饰符中使用不同的代码。答案已更新。
    【解决方案2】:

    尾随图标的点击处理程序必须使用空字符串调用 TextField 的 onValueChange:

           ...
           trailingIcon = {Icon(Icons.Filled.Email, contentDescription = "sdsd", modifier = Modifier.offset(x= 10.dp).clickable {
               onValueChange("") // just send an update that the field is now empty
           })}
           ...
    

    【讨论】:

    • 优雅而合乎逻辑
    【解决方案3】:

    这将实现这一目标

    //Caller
    val text by remember { mutableStateOf (...) }
    
    TextInput(text, ..., ...,)
    
    
    //Composable
    @Composable
    fun TextInput(text, ..., ...){
    val textState by remember { mutableStateOf (text) }
    TextField(
     value = textState,
    trailingIcon = {
     Icon(..., Modifier.clickable { textState = "" })
    }
    }
    
    

    【讨论】:

      【解决方案4】:

      使用trailingIcon 可组合属性和IconButton 将图标的背景选择器与主题的其余部分匹配。也可以设置为空条件,仅在文本字段中有输入时才显示。

      下面是示例代码sn-p:

      var text by remember { mutableStateOf ("") }
      
      TextField(
          trailingIcon = {
              when {
                  text.isNotEmpty() -> IconButton(onClick = {
                      text = ""
                  }) {
                      Icon(
                          imageVector = Icons.Filled.Clear,
                          contentDescription = "Clear"
                      )
                  }
              }
          }
      )
      

      【讨论】:

        猜你喜欢
        • 2022-11-01
        • 1970-01-01
        • 2021-12-04
        • 2021-11-01
        • 2022-06-27
        • 1970-01-01
        • 2022-06-29
        • 2023-01-31
        • 1970-01-01
        相关资源
        最近更新 更多