Jetpack compose:将 drawable 添加到 TextField 的开头

时间:2021-06-06 00:42:06

标签: android textfield android-jetpack-compose

在下面显示的代码中,我有一个接受用户输入的 TextField。如何将 drawable 添加到 TextField 的开头或结尾?我找不到任何用于设置 drawableStartdrawableEnd 的属性。

var text by rememberSaveable{ mutableStateOf("") }

TextField(
    value = text,
    modifier = Modifier
        .fillMaxWidth(1f)
        .padding(30.dp),
    onValueChange = { text = it },
    placeholder = { Text(text = "Email") },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
)

1 个答案:

答案 0 :(得分:1)

TextField documentation 中有 leadingIcontrailingIcon 属性。使用 leadingIcon 代替 drawableStarttrailingIcon 代替 drawableEnd。在下面找到示例实现:

var text by rememberSaveable{ mutableStateOf("") }

TextField(
    value = text,
    modifier = Modifier
        .fillMaxWidth(1f)
        .padding(30.dp),
    onValueChange = { text = it },
    placeholder = { Text(text = "Email") },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
    leadingIcon = { Icon(Icons.Filled.Favorite, contentDescription = "Localized description") },
    trailingIcon = { Icon(Icons.Filled.Info, contentDescription = "Localized description") }
)