![]()
![]()
前言
表单组件是用户输入和交互的核心,小分享 App 中使用了 TextInput(搜索框)、TextArea(文字编辑)等表单组件。本篇集中讲解它们的核心属性和事件处理。详细 API 可参考 HarmonyOS TextInput 官方文档。
一、TextInput 组件
1.1 基本用法
TextInput({placeholder:'搜索内容、模板、用户',text:this.searchKey}).fontSize(14).backgroundColor(Color.Transparent).layoutWeight(1).onChange((value:string)=>{this.searchKey=value})
1.2 TextInput 常用属性
| 属性 | 作用 | 示例 |
|---|
placeholder | 占位符 | '请输入...' |
text | 初始文本 | this.searchKey |
onChange | 内容变化 | (value) => {} |
type | 输入类型 | InputType.Normal |
maxLength | 最大长度 | 50 |
enterKeyType | 回车键类型 | EnterKeyType.Search |
1.3 搜索框容器
Row({space:8}){Text('🔍').fontSize(16).fontColor('#999999')TextInput({placeholder:'搜索...',text:this.searchKey}).fontSize(14).backgroundColor(Color.Transparent).layoutWeight(1).onChange((value:string)=>{this.searchKey=value})}.width('100%').height(40).backgroundColor('#F5F5F5').borderRadius(20).padding({left:16,right:16})
二、TextArea 组件
2.1 基本用法
TextArea({text:'初始内容',placeholder:'输入你想分享的内容...'}).fontSize(15).fontColor('#333333').backgroundColor(Color.Transparent).width('100%').height(300).padding({left:16,right:16}).onChange((value:string)=>{this.textContent=value})
2.2 TextArea 常用属性
| 属性 | 作用 | 示例 |
|---|
text | 初始文本 | '内容' |
placeholder | 占位符 | '输入...' |
onChange | 内容变化 | (value) => {} |
maxLength | 最大字符数 | 500 |
editable | 是否可编辑 | true |
三、TextPicker 组件
3.1 基本用法
TextPicker({range:['小','中','大'],selected:this.selectedSize}).onChange((value:string,index:number)=>{this.selectedSize=index})
3.2 TextPicker 常用属性
| 属性 | 作用 | 示例 |
|---|
range | 选项列表 | ['小','中','大'] |
selected | 选中索引 | 0 |
onChange | 值变化 | (value, index) => {} |
四、表单组件对比
| 组件 | 用途 | 行数 | 适用场景 |
|---|
| TextInput | 单行输入 | 1 行 | 搜索框、用户名 |
| TextArea | 多行输入 | 多行 | 文章编辑、评论 |
| TextPicker | 选择器 | 1 行 | 选项选择 |
五、本文核心知识点
5.1 表单组件核心要点
- TextInput:单行输入,搜索框
- TextArea:多行输入,文章编辑
- TextPicker:选择器,选项选择
5.2 实战开发要点
- TextInput 用 onChange 实现双向绑定
- TextArea 设置 height 控制展开高度
- 搜索框用 borderRadius(20) 圆角设计
相关资源
- HarmonyOS TextInput 官方文档
- HarmonyOS TextArea 组件
- HarmonyOS TextPicker 组件
- 开源鸿蒙跨平台社区
附录:表单组件的完整实现细节
1. TextInput 组件完整属性
| 属性 | 作用 | 示例 |
|---|
placeholder | 占位符 | '请输入...' |
text | 初始文本 | this.searchKey |
onChange | 内容变化 | (value) => {} |
type | 输入类型 | InputType.Normal |
maxLength | 最大长度 | 50 |
enterKeyType | 回车键类型 | EnterKeyType.Search |
2. TextArea 组件完整属性
TextArea({text:'初始内容',placeholder:'输入你想分享的内容...'}).fontSize(15).fontColor('#333333').backgroundColor(Color.Transparent).width('100%').height(300).padding({left:16,right:16}).onChange((value:string)=>{this.textContent=value})
3. TextPicker 组件
TextPicker({range:['小','中','大'],selected:this.selectedSize}).onChange((value:string,index:number)=>{this.selectedSize=index})
4. 表单组件对比
| 组件 | 用途 | 行数 | 适用场景 |
|---|
| TextInput | 单行输入 | 1 行 | 搜索框、用户名 |
| TextArea | 多行输入 | 多行 | 文章编辑、评论 |
| TextPicker | 选择器 | 1 行 | 选项选择 |
5. 完整代码文件索引
6. 总结
本文详细讲解了 ArkUI 三大表单组件 TextInput、TextArea、TextPicker 的完整属性和用法。
相关资源
- HarmonyOS TextInput 官方文档:TextInput Reference
- HarmonyOS TextArea 组件:TextArea Reference
- HarmonyOS TextPicker 组件:TextPicker Reference
- HarmonyOS 表单开发:Form Development
- HarmonyOS 输入组件:Input Components
- HarmonyOS 选择器:Picker Components
- HarmonyOS 状态管理:State Management
- 开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
本文涉及的所有 API 索引
| API | 类别 | 用途 |
|---|
| Column | 布局容器 | 垂直排列子组件 |
| Row | 布局容器 | 水平排列子组件 |
| Text | 基础组件 | 显示文本 |
| ForEach | 渲染控制 | 循环渲染列表 |
| @State | 装饰器 | 组件内状态管理 |
| @Builder | 装饰器 | 封装可复用 UI 片段 |
| router.pushUrl | 路由 | 页面跳转 |
| router.back | 路由 | 返回上一页 |
总结
本文详细讲解了小分享 App 中对应页面的完整实现。核心知识点涵盖布局容器、组件封装、状态管理、路由跳转等关键技术。通过本文的学习,读者可以掌握 HarmonyOS ArkUI 声明式开发的核心技能,并能够独立实现类似的页面功能。
相关资源
- HarmonyOS 官方文档:https://developer.huawei.com/consumer/cn/doc/
- ArkTS 语法指南:ArkTS Introduction
- 状态管理指南:State Management
- ArkUI 组件参考:ArkUI Components
- 路由 API:Router API
- 开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
6. TextInput 完整属性
// 搜索框 - 圆角设计TextInput({placeholder:'搜索内容、模板、用户',text:this.searchKey}).fontSize(14).backgroundColor(Color.Transparent).layoutWeight(1).onChange((value:string)=>{this.searchKey=value})// 带搜索图标的搜索框Row({space:8}){Text('🔍').fontSize(16).fontColor('#999999')TextInput({placeholder:'搜索...',text:this.searchKey}).layoutWeight(1)}.width('100%').height(40).backgroundColor('#F5F5F5').borderRadius(20).padding({left:16,right:16})
7. TextArea 完整属性
// 文字编辑器 - 多行输入TextArea({text:'生活的美好在于分享...',placeholder:'输入你想分享的内容...'}).fontSize(15).fontColor('#333333').backgroundColor(Color.Transparent).width('100%').height(300).padding({left:16,right:16}).onChange((value:string)=>{this.textContent=value})
8. TextPicker 完整属性
// 字体大小选择器TextPicker({range:['12','14','16','18','20','24','28','32'],selected:this.selectedSize}).onChange((value:string,index:number)=>{this.selectedSize=index})
9. 表单组件对比
| 组件 | 用途 | 行数 | 适用场景 |
|---|
| TextInput | 单行输入 | 1 行 | 搜索框、用户名 |
| TextArea | 多行输入 | 多行 | 文章编辑、评论 |
| TextPicker | 选择器 | 1 行 | 选项选择 |
10. 小分享 App 中的使用
| 页面 | 组件 | 用途 |
|---|
| DiscoverPage | TextInput | 搜索框 |
| TextEditPage | TextArea | 文字编辑 |
| LinkEditPage | TextInput | 链接输入 |
| SettingsPage | TextPicker | 选项选择 |
11. 完整代码文件索引
12. 本文涉及的所有 API
| API/组件 | 用途 | 文档链接 |
|---|
| TextInput | 单行输入 | TextInput |
| TextArea | 多行输入 | TextArea |
| TextPicker | 选择器 | TextPicker |
| InputType | 输入类型 | TextInput |
| EnterKeyType | 回车键类型 | TextInput |
13. 实现要点总结
表单组件核心要点:
- TextInput:单行输入,支持 placeholder、onChange、maxLength
- TextArea:多行输入,支持固定高度、自动滚动
- TextPicker:选择器,支持 range 和 selected 绑定
- @State 绑定:onChange 回调中更新状态变量
14. 总结
本文详细讲解了 ArkUI 三大表单组件 TextInput、TextArea、TextPicker 的完整属性和用法。
相关资源
- HarmonyOS TextInput 官方文档:TextInput Reference
- HarmonyOS TextArea 组件:TextArea Reference
- HarmonyOS TextPicker 组件:TextPicker Reference
- HarmonyOS 表单开发:Form Development
- HarmonyOS 输入组件:Input Components
- HarmonyOS 选择器:Picker Components
- HarmonyOS 状态管理:State Management
- 开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
15. 完整代码文件索引
| 文件路径 | 说明 |
|---|
pages/DiscoverPage.ets | 搜索框 TextInput |
pages/TextEditPage.ets | 文字编辑 TextArea |
pages/LinkEditPage.ets | 链接输入 TextInput |
pages/SettingsPage.ets | 选项选择 TextPicker |
16. 本文涉及的所有 API
| API/组件 | 用途 | 文档链接 |
|---|
| TextInput | 单行输入 | TextInput |
| TextArea | 多行输入 | TextArea |
| TextPicker | 选择器 | TextPicker |
| @State | 状态绑定 | State |
| onChange | 变化回调 | 各组件文档 |
17. 实现要点总结
表单组件核心要点:
- TextInput:单行输入,支持 placeholder、onChange、maxLength、enterKeyType
- TextArea:多行输入,支持固定高度、自动滚动、placeholder
- TextPicker:选择器,支持 range 数组和 selected 索引绑定
- @State 绑定:onChange 回调中更新状态变量,实现双向绑定
18. 总结
本文详细讲解了 ArkUI 三大表单组件 TextInput、TextArea、TextPicker 的完整属性和用法。
相关资源
- HarmonyOS TextInput 官方文档:TextInput Reference
- HarmonyOS TextArea 组件:TextArea Reference
- HarmonyOS TextPicker 组件:TextPicker Reference
- HarmonyOS 表单开发:Form Development
- HarmonyOS 输入组件:Input Components
- HarmonyOS 选择器:Picker Components
- HarmonyOS 状态管理:State Management
- 开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
如果这篇文章对你有帮助,欢迎点赞👍、收藏⭐、关注🔔,你的支持是我持续创作的动力!