Skip to content

TextField ​

带标签的单行文本输入框。标签位于控件上方;帮助或错误文本位于下方。用它来输入简短的自由格式值,例如姓名、邮箱或密码。

导入 ​

kotlin
import io.github.heartcoolman.prism.ui.PrismTextField

基本用法 ​

kotlin
var text by remember { mutableStateOf("") }
PrismTextField(
    value = text,
    onValueChange = { text = it },
    label = "Email",
    placeholder = "you@example.com",
)

状态 ​

非空的 error 会将边框和消息切换为危险样式,并替换 helpText。enabled = false 渲染为禁用样式。

kotlin
// Help text below the field
PrismTextField(
    value = text,
    onValueChange = { text = it },
    label = "Username",
    helpText = "Pick a unique handle.",
)

// Error state
PrismTextField(
    value = text,
    onValueChange = { text = it },
    label = "Email",
    error = "Invalid email address.",
)

// Disabled
PrismTextField(
    value = text,
    onValueChange = { text = it },
    label = "Locked",
    enabled = false,
)

前导图标与密码 ​

kotlin
PrismTextField(
    value = text,
    onValueChange = { text = it },
    label = "Password",
    leadingIcon = { PrismIcon(PrismIcons.Lock, contentDescription = null, size = 18.dp) },
    visualTransformation = PasswordVisualTransformation(),
)

API ​

参数类型默认值说明
valueString—当前文本值。
onValueChange(String) -> Unit—每次编辑时以新值调用。
modifierModifierModifier应用于该列的布局 Modifier。
labelString?null渲染在输入框上方的标签。
placeholderString?null值为空时显示的占位符。
helpTextString?null非错误状态时显示在下方的辅助文本。
errorString?null错误消息;非空时将字段切换到错误状态并替换 helpText。
leadingIcon(@Composable () -> Unit)?null渲染在输入框内部、左对齐的字形。
enabledBooleantrue字段是否接受输入。
singleLineBooleantrue将输入限制为单行。
fullWidthBooleanfalse将字段拉伸以填满容器宽度。
keyboardOptionsKeyboardOptionsKeyboardOptions.Default软键盘配置(类型、IME 操作)。
visualTransformationVisualTransformationVisualTransformation.None应用于文本的视觉变换,例如密码掩码。

基于 MIT 许可发布。