
https://www.bilibili.com/video/BV1jomdBBE4H/

PasswordInput 是控件库中专用于密码输入的组件,基于 TextInput 扩展而来,支持显示/隐藏密码切换、密码强度显示、验证等功能,适用于登录、注册、密码修改等场景。
密码输入框采用安全易用设计,具有以下特点:
import { PasswordInput } from '../components/base'
@Entry
@Component
struct MyPage {
@State password: string = ''
build() {
Column({ space: 20 }) {
// 基础密码输入框
PasswordInput({
value: $password,
placeholder: '请输入密码'
})
// 带标签的密码输入框
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码'
})
// 带密码强度的密码输入框
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码',
showStrength: true
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}PasswordInput 使用 @Link 实现双向绑定,需要使用 $variableName 语法:
@State password: string = ''
PasswordInput({
value: $password // 使用 $ 创建双向绑定
})属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | @Link string | - | 密码值(必需,双向绑定) |
placeholder | string | '请输入密码' | 占位符文本 |
label | string? | undefined | 标签文本(可选) |
hint | string? | undefined | 提示文本(可选,显示在输入框下方) |
errorMessage | string? | undefined | 错误提示文本(可选,优先级高于 hint) |
inputSize | 'small' | 'medium' | 'large' | 'medium' | 输入框尺寸 |
disabled | boolean | false | 是否禁用 |
readonly | boolean | false | 是否只读 |
required | boolean | false | 是否必填 |
maxLength | number? | undefined | 最大长度 |
showStrength | boolean | false | 是否显示密码强度 |
showToggleButton | boolean | true | 是否显示显示/隐藏切换按钮 |
showBrand | boolean | true | 是否显示品牌标识 |
inputWidth | string | number? | undefined | 输入框宽度 |
值 | 说明 |
|---|---|
None | 无(空密码) |
Weak | 弱 |
Medium | 中 |
Strong | 强 |
尺寸 | 高度 | 字体大小 | 图标大小 | 内边距(左右) |
|---|---|---|---|---|
small | 48vp | 14vp | 18vp | 12vp |
medium | 60vp | 16vp | 20vp | 14vp |
large | 72vp | 18vp | 24vp | 16vp |
@Entry
@Component
struct PasswordExample1 {
@State password: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password,
placeholder: '请输入密码'
})
Text(`密码长度:${this.password.length}`)
.fontSize(14)
.fontColor('#666')
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample2 {
@State password: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码',
hint: '密码长度为8-20个字符'
})
PasswordInput({
value: $password,
placeholder: '请确认密码',
label: '确认密码',
errorMessage: '两次输入的密码不一致'
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample3 {
@State password: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码',
showStrength: true,
hint: '密码应包含大小写字母、数字和特殊字符'
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample4 {
@State password1: string = ''
@State password2: string = ''
@State password3: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password1,
placeholder: '小尺寸',
inputSize: 'small'
})
PasswordInput({
value: $password2,
placeholder: '中等尺寸(默认)',
inputSize: 'medium'
})
PasswordInput({
value: $password3,
placeholder: '大尺寸',
inputSize: 'large'
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample5 {
@State password1: string = ''
@State password2: string = '已设置密码'
@State password3: string = '只读密码'
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password1,
placeholder: '请输入密码',
label: '密码',
required: true
})
PasswordInput({
value: $password2,
placeholder: '请输入密码',
label: '禁用状态',
disabled: true
})
PasswordInput({
value: $password3,
placeholder: '请输入密码',
label: '只读状态',
readonly: true
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample6 {
@State password: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password,
placeholder: '显示切换按钮(默认)',
label: '密码',
showToggleButton: true
})
PasswordInput({
value: $password,
placeholder: '隐藏切换按钮',
label: '密码',
showToggleButton: false
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct PasswordExample7 {
@State password: string = ''
build() {
Column({ space: 15 }) {
PasswordInput({
value: $password,
placeholder: '最多输入20个字符',
label: '密码',
maxLength: 20,
hint: `已输入 ${this.password.length}/20 个字符`
})
}
.width('100%')
.padding(20)
}
}@Entry
@Component
struct LoginForm {
@State username: string = ''
@State password: string = ''
build() {
Column({ space: 20 }) {
Text('用户登录')
.fontSize(24)
.fontWeight(FontWeight.Bold)
TextInput({
value: $username,
placeholder: '请输入用户名',
label: '用户名',
required: true
})
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码',
required: true
})
Button('登录')
.width('100%')
.height(44)
.onClick(() => {
// 处理登录逻辑
})
}
.width('100%')
.padding(30)
}
}@Entry
@Component
struct RegisterForm {
@State username: string = ''
@State password: string = ''
@State confirmPassword: string = ''
build() {
Column({ space: 20 }) {
Text('用户注册')
.fontSize(24)
.fontWeight(FontWeight.Bold)
TextInput({
value: $username,
placeholder: '请输入用户名',
label: '用户名',
required: true
})
PasswordInput({
value: $password,
placeholder: '请输入密码',
label: '密码',
required: true,
showStrength: true,
hint: '密码应包含大小写字母、数字和特殊字符'
})
PasswordInput({
value: $confirmPassword,
placeholder: '请确认密码',
label: '确认密码',
required: true,
errorMessage: this.password !== this.confirmPassword && this.confirmPassword ? '两次输入的密码不一致' : ''
})
Button('注册')
.width('100%')
.height(44)
.onClick(() => {
// 处理注册逻辑
})
}
.width('100%')
.padding(30)
}
}PasswordInput 的所有样式都通过 ComponentTheme 配置,所有配置都在代码中,不依赖JSON文件。
import { ComponentTheme } from '../theme/ComponentTheme'
// 修改主色(影响聚焦状态的边框颜色)
ComponentTheme.PRIMARY_COLOR = '#007AFF'
// 修改错误色(影响错误状态的边框和提示颜色)
ComponentTheme.ERROR_COLOR = '#FF3B30'
// 修改边框颜色
ComponentTheme.BORDER_COLOR = '#E5E5E5'
// 修改圆角
ComponentTheme.BORDER_RADIUS = 8import { ComponentTheme } from '../theme/ComponentTheme'
// 使用 setTheme 方法批量配置
ComponentTheme.setTheme({
primaryColor: '#007AFF',
errorColor: '#FF3B30',
borderRadius: 8,
spacing: 16
})推荐:根据使用场景选择尺寸
showStrength,帮助用户创建安全密码showStrength,简化界面showStrength,确保新密码安全showToggleButton: true,方便用户确认输入maxLength 限制最大长度errorMessage 显示验证错误showStrength 提示用户密码强度hint 提供密码要求说明errorMessage 显示明确的错误信息A: 当前密码强度计算规则是内置的,基于以下因素:
如果需要自定义规则,可以在外部监听 value 变化,自行计算强度并显示。
A: 设置 showToggleButton: false:
PasswordInput({
value: $password,
showToggleButton: false
})A: 设置 showStrength: false(默认值):
PasswordInput({
value: $password,
showStrength: false
})A: 密码强度根据以下规则计算:
条件包括:长度≥8、小写字母、大写字母、数字、特殊字符。
A: 使用 inputWidth 属性:
PasswordInput({
value: $password,
inputWidth: 300 // 固定宽度
})
PasswordInput({
value: $password,
inputWidth: '100%' // 百分比宽度
})A: 通过监听两个密码输入框的值进行比较:
@State password: string = ''
@State confirmPassword: string = ''
PasswordInput({
value: $password,
label: '密码'
})
PasswordInput({
value: $confirmPassword,
label: '确认密码',
errorMessage: this.password !== this.confirmPassword && this.confirmPassword ? '两次输入的密码不一致' : ''
})PasswordInput 是控件库中专用于密码输入的组件,具有以下核心特性:
$variableName 创建双向绑定showStrength 显示密码强度showToggleButton 控制显示/隐藏按钮label 属性添加标签hint 或 errorMessage 显示提示inputSize 属性选择合适尺寸ComponentTheme 自定义全局样式下一篇预告:NumberInput(数字输入框)详解
本文档属于《鸿蒙PC UI控件库开发系列文章》第8篇