首页 > 解决方案 > Jetpack compose:将可绘制对象添加到 TextField 的开头

问题描述

在下面显示的代码中,我有一个接受用户输入的 TextField。如何在 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)
)

标签: androidtextfieldandroid-jetpack-compose

解决方案


TextField 文档中有一个leadingIconandtrailingIcon属性。使用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") }
)

推荐阅读