跳转到内容

TextField ​

TextField 是 Miuix 中的基础输入组件,用于接收用户的文本输入。组件提供了丰富的自定义选项,支持标签动画、前置和后置图标等功能。

引入 ​

kotlin
import top.yukonga.miuix.kmp.basic.TextField

基本用法 ​

TextField 组件可以用于获取用户输入:

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "用户名"
)

信息

此 TextFiled 组件现在也支持最新基于状态的版本,具体请参考 State-based 文档。

输入框类型 ​

带标签输入框 ​

标签会在输入框获得焦点或有内容时自动移动到顶部:

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "邮箱地址"
)

使用标签作为占位符 ​

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "请输入内容",
    useLabelAsPlaceholder = true
)

组件状态 ​

禁用状态 ​

kotlin
var text by remember { mutableStateOf("") }
TextField(
    value = text,
    onValueChange = { text = it },
    label = "禁用输入框",
    enabled = false
)

只读状态 ​

kotlin
var text by remember { mutableStateOf("这是只读内容") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "只读输入框",
    readOnly = true
)

属性 ​

TextField 属性 ​

属性名类型说明默认值是否必须
valueString 或 TextFieldValue输入框的文本值-是
onValueChange(String) -> Unit 或 (TextFieldValue) -> Unit文本变化时的回调函数-是
modifierModifier应用于输入框的修饰符Modifier否
insideMarginDpSize输入框内部边距TextFieldDefaults.InsideMargin否
colorsTextFieldColors输入框使用的颜色TextFieldDefaults.textFieldColors()否
cornerRadiusDp输入框圆角半径TextFieldDefaults.CornerRadius否
labelString输入框标签文本""否
useLabelAsPlaceholderBoolean是否使用标签作为占位符false否
enabledBoolean输入框是否可用true否
readOnlyBoolean输入框是否只读false否
textStyleTextStyle输入文本样式MiuixTheme.textStyles.main否
keyboardOptionsKeyboardOptions键盘选项配置KeyboardOptions.Default否
keyboardActionsKeyboardActions键盘操作配置KeyboardActions.Default否
leadingIcon@Composable (() -> Unit)?前置图标null否
trailingIcon@Composable (() -> Unit)?后置图标null否
singleLineBoolean是否为单行输入false否
maxLinesInt最大行数如果 singleLine 为 true 则为 1,否则为 Int.MAX_VALUE否
minLinesInt最小行数1否
visualTransformationVisualTransformation视觉转换器VisualTransformation.None否
onTextLayout(TextLayoutResult) -> Unit文本布局变化回调{}否
interactionSourceMutableInteractionSource?交互源null否
cursorBrushBrush光标画刷SolidColor(colors.borderColor)否

TextField(state-based)属性 ​

属性名类型说明默认值是否必须
stateTextFieldState保存文本与选择的状态对象-是
modifierModifier应用于输入框的修饰符Modifier否
insideMarginDpSize输入框内部边距TextFieldDefaults.InsideMargin否
colorsTextFieldColors输入框使用的颜色TextFieldDefaults.textFieldColors()否
cornerRadiusDp输入框圆角半径TextFieldDefaults.CornerRadius否
labelString输入框标签文本""否
useLabelAsPlaceholderBoolean是否使用标签作为占位符false否
enabledBoolean输入框是否可用true否
readOnlyBoolean输入框是否只读false否
inputTransformationInputTransformation?输入变换器null否
textStyleTextStyle输入文本样式MiuixTheme.textStyles.main否
keyboardOptionsKeyboardOptions键盘选项配置KeyboardOptions.Default否
onKeyboardActionKeyboardActionHandler?键盘动作处理器null否
lineLimitsTextFieldLineLimits行数限制TextFieldLineLimits.Default否
leadingIcon@Composable (() -> Unit)?前置图标null否
trailingIcon@Composable (() -> Unit)?后置图标null否
onTextLayoutDensity.(getResult: () -> TextLayoutResult?) -> Unit文本布局回调(带 Density 接收)null否
interactionSourceMutableInteractionSource?交互源null否
cursorBrushBrush光标画刷SolidColor(colors.borderColor)否
outputTransformationOutputTransformation?输出变换器null否
scrollStateScrollState滚动状态rememberScrollState()否

TextFieldDefaults 对象 ​

TextFieldDefaults 对象提供了 TextField 组件的默认值。

常量 ​

常量名类型说明默认值
CornerRadiusDp输入框圆角半径16.dp
InsideMarginDpSize输入框内部边距DpSize(16.dp, 16.dp)

textFieldColors() 工厂 ​

构造 [TextFieldColors] 实例。按需覆盖任意子集,未指定的参数回退到 Miuix 主题默认值。

参数类型默认值
backgroundColorColorMiuixTheme.colorScheme.secondaryContainer
labelColorColorMiuixTheme.colorScheme.onSecondaryContainer
borderColorColorMiuixTheme.colorScheme.primary

进阶用法 ​

带图标输入框 ​

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "搜索",
    leadingIcon = {
        Icon(
            imageVector = MiuixIcons.Search,
            contentDescription = "搜索图标",
            modifier = Modifier.padding(horizontal = 12.dp)
        )
    }
)

密码输入框 ​

kotlin
var password by remember { mutableStateOf("") }
var passwordVisible by remember { mutableStateOf(false) }

TextField(
    value = password,
    onValueChange = { password = it },
    label = "密码",
    visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
    trailingIcon = {
        IconButton(
            onClick = { passwordVisible = !passwordVisible },
            modifier = Modifier.padding(end = 12.dp)
        ) {
            Icon(
                imageVector = MiuixIcons.Rename,
                tint = if (passwordVisible) MiuixTheme.colorScheme.primary else MiuixTheme.colorScheme.onSecondaryContainer,
                contentDescription = if (passwordVisible) "隐藏密码" else "显示密码"
            )
        }
    }
)

带验证的输入框 ​

kotlin
var email by remember { mutableStateOf("") }
var isError by remember { mutableStateOf(false) }
val errorColor = Color.Red.copy(0.3f)
val emailPattern = remember { Regex("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+") }

Column {
    TextField(
        value = email,
        onValueChange = {
            email = it
            isError = email.isNotEmpty() && !emailPattern.matches(email)
        },
        label = "电子邮箱",
        colors = TextFieldDefaults.textFieldColors(
            labelColor = if (isError) errorColor else MiuixTheme.colorScheme.onSecondaryContainer,
        ),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
    )
    if (isError) {
        Text(
            text = "请输入有效的邮箱地址",
            color = errorColor,
            style = MiuixTheme.textStyles.body2,
            modifier = Modifier.padding(start = 16.dp, top = 4.dp)
        )
    }
}

自定义样式 ​

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "自定义输入框",
    cornerRadius = 8.dp,
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = MiuixTheme.colorScheme.primary.copy(alpha = 0.1f),
    ),
    textStyle = TextStyle(
        fontWeight = FontWeight.Medium,
        fontSize = 16.sp,
        color = MiuixTheme.colorScheme.primary
    )
)

使用 TextFieldValue ​

当需要更细致地控制文本选择和光标位置时:

kotlin
var textFieldValue by remember { mutableStateOf(TextFieldValue("")) }

TextField(
    value = textFieldValue,
    onValueChange = { textFieldValue = it },
    label = "高级输入控制",
    // TextFieldValue 提供了对文本、选择范围和光标位置的控制
)

变更日志

基于 Apache-2.0 许可发布