TextInput 单行文本输入框
TextInput({ placeholder: '请输入用户名' }) // 占位提示
.type(InputType.Normal) // 输入类型(Normal、Number、Email、Url、PhoneNumber、Password)
.fontSize(16) // 字体大小
.fontColor('#333') // 字体颜色
.textAlign(TextAlign.Start) // 对齐方式
.caretColor('#000') // 光标颜色
.placeholderColor('#999') // 占位文字颜色
.enterKeyType(EnterKeyType.Done) // 回车键样式(Go、Next、Send、Search、Done)
.enabled(true) // 是否可用
.maxLength(50) // 最大输入长度
.width('100%')
.height(48)
.backgroundColor('#F0F0F0')
.padding(8)
.borderRadius(6)
PasswordInput 密码输入框
TextInput({ placeholder: '请输入密码' })
.type(InputType.Password) // 设置为密码输入
.fontSize(16)
.fontColor('#000')
.placeholderColor('#888')
.enterKeyType(EnterKeyType.Done)
.maxLength(20)
.width('100%')
.height(48)
.padding(10)
.borderRadius(6)
.backgroundColor('#F9F9F9')
TextArea 多行文本输入框
TextInput({ placeholder: '请输入备注内容' })
.type(InputType.Normal)
.fontSize(16)
.fontColor('#333')
.placeholderColor('#AAA')
.textAlign(TextAlign.Start)
.textInputStyle({ inputFilter: (text) => text }) // 输入过滤器
.maxLines(5) // 最多显示行数
.minLines(3) // 最少显示行数
.maxLength(300)
.width('100%')
.height(120)
.padding(12)
.borderRadius(8)
.backgroundColor('#EFEFEF')
Checkbox 单个复选框
Checkbox({
name: 'accept', // 名称
group: '', // 所属组名
value: 'yes' // 值
})
.selected(true) // 默认选中
.enabled(true) // 是否启用
.width(24)
.height(24)
.selectedColor('#007DFF') // 选中状态颜色
.unselectedColor('#CCCCCC') // 未选中状态颜色
Text('我已阅读并同意') // 可配合文字使用
.fontSize(14)
.margin({ left: 8 })
CheckboxGroup 复选框组
CheckboxGroup({
group: 'hobbies',
selected: ['movie', 'music'] // 默认选中的值列表
}) {
Checkbox({ name: 'hobbies', value: 'movie' })
.selectedColor('#1E90FF')
Text('电影').fontSize(16)
Checkbox({ name: 'hobbies', value: 'music' })
.selectedColor('#1E90FF')
Text('音乐').fontSize(16)
Checkbox({ name: 'hobbies', value: 'reading' })
.selectedColor('#1E90FF')
Text('阅读').fontSize(16)
}
Radio 单个单选按钮
Radio({
name: 'gender',
value: 'male' // 当前选项值
})
.selected(true) // 是否被选中
.enabled(true)
.width(24)
.height(24)
.selectedColor('#00BFFF')
.unselectedColor('#CCCCCC')
Text('男').fontSize(16).margin({ left: 8 })
RadioGroup 单选按钮组
RadioGroup({
group: 'gender',
selected: 'female' // 默认选中项
}) {
Radio({ name: 'gender', value: 'male' })
.selectedColor('#007DFF')
Text('男').fontSize(16)
Radio({ name: 'gender', value: 'female' })
.selectedColor('#007DFF')
Text('女').fontSize(16)
}
Switch 开关组件
Toggle()
.checked(true) // 是否打开
.enabled(true) // 是否可操作
.onToggle((isOn) => {
console.log('开关状态:', isOn)
})
.selectedColor('#00CC66') // 打开状态颜色
.switchPointColor('#FFFFFF') // 滑块颜色
.width(50)
.height(30)
Slider 滑动条组件
Slider({
value: 50, // 当前值
min: 0,
max: 100,
step: 1
})
.showTips(true) // 显示当前值提示
.style(SliderStyle.Outset) // 滑动条样式(Outset / Inset)
.trackColor('#D3D3D3') // 背景轨道颜色
.selectedColor('#1E90FF') // 已选部分颜色
.blockColor('#FFFFFF') // 滑块颜色
.blockSize(20) // 滑块大小
.width('100%')
.onChange((val) => {
console.log('当前值:', val)
})
DatePicker 日期选择器
DatePicker({
start: new Date('2000-01-01'), // 可选开始日期
end: new Date('2030-12-31'), // 可选结束日期
selected: new Date('2023-01-01') // 默认选中日期
})
.lunar(false) // 是否使用农历
.disappearTextStyle({ color: '#CCCCCC' }) // 不可选项样式
.textStyle({ fontSize: 16, color: '#000000' }) // 文本样式
.onChange((date) => {
console.log('选择的日期:', date)
})
TimePicker 时间选择器
TimePicker({
use12HourClock: false, // 是否使用 12 小时制
selected: { hour: 14, minute: 30 } // 默认时间
})
.textStyle({ fontSize: 16, color: '#000000' }) // 文本样式
.disappearTextStyle({ color: '#CCCCCC' }) // 不可选样式
.onChange((time) => {
console.log(`选择的时间: ${time.hour}:${time.minute}`)
})