news 2026/5/23 23:58:19

鸿蒙PC UI控件库 - SearchInput 搜索输入框详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
鸿蒙PC UI控件库 - SearchInput 搜索输入框详解

视频演示地址:

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

📋 目录

  • 概述
  • 特性
  • 快速开始
  • API 参考
  • 使用示例
  • 主题配置
  • 最佳实践
  • 常见问题
  • 总结

概述

SearchInput是控件库中专用于搜索场景的输入框组件,支持清除按钮、历史记录、搜索按钮等功能,适用于搜索框、筛选输入、快速查找等场景。

设计理念

搜索输入框采用便捷高效设计,具有以下特点:

  1. 搜索图标:左侧自动显示搜索图标,直观明了
  2. 清除按钮:支持一键清除输入内容
  3. 历史记录:支持显示搜索历史,快速复用
  4. 搜索按钮:支持显示搜索按钮,明确操作
  5. 品牌标识:左下角自动包含品牌标识(圆圈红字"PC")
  6. 主题统一:所有样式配置都在代码中,方便定制

适用场景

  • 搜索功能:全局搜索、内容搜索
  • 筛选输入:数据筛选、条件输入
  • 快速查找:列表查找、表格搜索
  • 历史复用:搜索历史、常用关键词

特性

✨ 核心特性

  • 搜索图标:左侧自动显示搜索图标
  • 清除按钮:支持一键清除输入内容
  • 搜索按钮:支持显示搜索按钮(可选)
  • 历史记录:支持显示搜索历史列表
  • 历史过滤:根据输入内容过滤历史记录
  • 多种尺寸:支持 small、medium、large 三种尺寸
  • 状态管理:支持禁用、只读等状态
  • 品牌标识:自动包含左下角品牌标识
  • 主题配置:所有样式都可通过代码配置

🎨 视觉特点

  • 正常状态:白色背景 + 灰色边框 + 搜索图标
  • 聚焦状态:主色边框高亮
  • 禁用状态:灰色背景 + 灰色文字 + 灰色边框
  • 只读状态:正常样式但不可编辑
  • 历史记录:下拉列表显示,支持点击选择

快速开始

基础用法

import{SearchInput}from'../components/base'@Entry @Component struct MyPage{@State searchValue:string=''build(){Column({space:20}){// 基础搜索输入框SearchInput({value:$searchValue,placeholder:'请输入搜索关键词'})// 带搜索按钮的搜索输入框SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showSearchButton:true})// 带历史记录的搜索输入框SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showHistory:true,historyItems:[{text:'鸿蒙开发'},{text:'UI控件库'},{text:'ArkTS'}]})}.width('100%').height('100%').padding(20).justifyContent(FlexAlign.Center)}}

关于双向绑定

SearchInput使用@Link实现双向绑定,需要使用$variableName语法:

@State searchValue:string=''SearchInput({value:$searchValue// 使用 $ 创建双向绑定})

API 参考

Props

属性名类型默认值说明
value@Link string-搜索值(必需,双向绑定)
placeholderstring'请输入搜索关键词'占位符文本
inputSize'small' | 'medium' | 'large''medium'输入框尺寸
disabledbooleanfalse是否禁用
readonlybooleanfalse是否只读
showClearButtonbooleantrue是否显示清除按钮
showSearchButtonbooleanfalse是否显示搜索按钮
showHistorybooleanfalse是否显示历史记录
historyItemsSearchHistoryItem[][]历史记录列表
maxHistoryCountnumber10最大历史记录数
showBrandbooleantrue是否显示品牌标识
inputWidthstring | number'100%'输入框宽度

SearchHistoryItem 接口

属性名类型说明
textstring历史记录文本(必需)
timestampnumber?时间戳(可选)

尺寸规格

尺寸高度字体大小图标大小内边距(左右)
small48vp14vp18vp12vp
medium60vp16vp20vp14vp
large72vp18vp24vp16vp

使用示例

1. 基础搜索输入框

@Entry @Component struct SearchExample1{@State searchValue:string=''build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:'请输入搜索关键词'})Text(`当前搜索:${this.searchValue||'(空)'}`).fontSize(14).fontColor('#666')}.width('100%').padding(20)}}

2. 带搜索按钮

@Entry @Component struct SearchExample2{@State searchValue:string=''build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showSearchButton:true})Text('提示:点击搜索按钮或按回车键执行搜索').fontSize(14).fontColor('#666')}.width('100%').padding(20)}}

3. 历史记录

@Entry @Component struct SearchExample3{@State searchValue:string=''@State historyItems:SearchHistoryItem[]=[{text:'鸿蒙开发',timestamp:Date.now()-3600000},{text:'UI控件库',timestamp:Date.now()-7200000},{text:'ArkTS',timestamp:Date.now()-10800000}]build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})Text('提示:输入关键词时会显示匹配的历史记录').fontSize(14).fontColor('#666')}.width('100%').padding(20)}}

4. 不同尺寸

@Entry @Component struct SearchExample4{@State search1:string=''@State search2:string=''@State search3:string=''build(){Column({space:15}){SearchInput({value:$search1,placeholder:'小尺寸搜索',inputSize:'small'})SearchInput({value:$search2,placeholder:'中等尺寸搜索(默认)',inputSize:'medium'})SearchInput({value:$search3,placeholder:'大尺寸搜索',inputSize:'large'})}.width('100%').padding(20)}}

5. 状态管理

@Entry @Component struct SearchExample5{@State search1:string=''@State search2:string='禁用搜索'@State search3:string='只读搜索'build(){Column({space:15}){SearchInput({value:$search1,placeholder:'正常状态'})SearchInput({value:$search2,placeholder:'请输入搜索关键词',disabled:true})SearchInput({value:$search3,placeholder:'请输入搜索关键词',readonly:true})}.width('100%').padding(20)}}

6. 隐藏清除按钮

@Entry @Component struct SearchExample6{@State searchValue:string=''build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:'不显示清除按钮',showClearButton:false})Text('提示:隐藏清除按钮后,需要手动删除输入内容').fontSize(14).fontColor('#666')}.width('100%').padding(20)}}

7. 完整功能示例

@Entry @Component struct SearchExample7{@State searchValue:string=''@State historyItems:SearchHistoryItem[]=[{text:'鸿蒙开发',timestamp:Date.now()-3600000},{text:'UI控件库',timestamp:Date.now()-7200000},{text:'ArkTS',timestamp:Date.now()-10800000},{text:'HarmonyOS',timestamp:Date.now()-14400000},{text:'组件开发',timestamp:Date.now()-18000000}]build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showSearchButton:true,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})Text('提示:支持搜索按钮和历史记录功能').fontSize(14).fontColor('#666')}.width('100%').padding(20)}}

8. 搜索页面示例

@Entry @Component struct SearchPage{@State searchValue:string=''@State searchResults:string[]=[]@State historyItems:SearchHistoryItem[]=[]privateperformSearch(){if(this.searchValue.trim().length>0){// 执行搜索逻辑this.searchResults=['结果1','结果2','结果3']// 添加到历史记录constnewItem:SearchHistoryItem={text:this.searchValue,timestamp:Date.now()}this.historyItems=[newItem,...this.historyItems].slice(0,10)}}build(){Column({space:20}){Text('搜索').fontSize(24).fontWeight(FontWeight.Bold)SearchInput({value:$searchValue,placeholder:'请输入搜索关键词',showSearchButton:true,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})if(this.searchResults.length>0){Column({space:10}){Text('搜索结果').fontSize(18).fontWeight(FontWeight.Bold)ForEach(this.searchResults,(result:string)=>{Text(result).fontSize(16).fontColor('#333').padding(10).width('100%').backgroundColor('#F5F5F5').borderRadius(8)})}.width('100%')}}.width('100%').padding(30)}}

主题配置

SearchInput 的所有样式都通过ComponentTheme配置,所有配置都在代码中,不依赖JSON文件。

修改默认颜色

import{ComponentTheme}from'../theme/ComponentTheme'// 修改主色(影响聚焦状态的边框颜色)ComponentTheme.PRIMARY_COLOR='#007AFF'// 修改错误色(影响错误状态的边框和提示颜色)ComponentTheme.ERROR_COLOR='#FF3B30'// 修改边框颜色ComponentTheme.BORDER_COLOR='#E5E5E5'// 修改圆角ComponentTheme.BORDER_RADIUS=8

批量配置

import{ComponentTheme}from'../theme/ComponentTheme'// 使用 setTheme 方法批量配置ComponentTheme.setTheme({primaryColor:'#007AFF',errorColor:'#FF3B30',borderRadius:8,spacing:16})

最佳实践

1. 尺寸选择

推荐:根据使用场景选择尺寸

  • small:用于紧凑空间、导航栏搜索
  • medium:默认尺寸,适用于大多数场景
  • large:用于重要搜索或大屏幕显示

2. 搜索按钮

  • 默认关闭showSearchButton: false,适用于大多数场景
  • 明确操作:需要明确搜索操作时,开启搜索按钮
  • 回车搜索:支持回车键触发搜索,无需按钮

3. 历史记录

  • 自动过滤:根据输入内容自动过滤历史记录
  • 数量限制:使用maxHistoryCount限制显示数量
  • 时间戳:可选添加时间戳,用于排序和显示

4. 清除按钮

  • 默认开启showClearButton: true,方便快速清除
  • 特殊场景:可以关闭清除按钮,强制用户手动删除

5. 历史记录管理

  • 本地存储:可以将历史记录保存到本地存储
  • 去重处理:添加新历史记录时去重
  • 数量限制:限制历史记录总数,避免过多

6. 搜索触发

  • 实时搜索:可以在onChange中实现实时搜索
  • 按钮搜索:点击搜索按钮或按回车键触发搜索
  • 防抖处理:建议对实时搜索进行防抖处理

常见问题

Q1: 如何禁用清除按钮?

A: 设置showClearButton: false

SearchInput({value:$searchValue,showClearButton:false})

Q2: 如何显示搜索按钮?

A: 设置showSearchButton: true

SearchInput({value:$searchValue,showSearchButton:true})

Q3: 如何实现历史记录功能?

A: 使用showHistoryhistoryItems属性:

@State historyItems:SearchHistoryItem[]=[{text:'关键词1'},{text:'关键词2'}]SearchInput({value:$searchValue,showHistory:true,historyItems:this.historyItems})

Q4: 如何限制历史记录数量?

A: 使用maxHistoryCount属性:

SearchInput({value:$searchValue,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5// 最多显示5条})

Q5: 如何设置输入框宽度?

A: 使用inputWidth属性:

SearchInput({value:$searchValue,inputWidth:300// 固定宽度})SearchInput({value:$searchValue,inputWidth:'100%'// 百分比宽度})

Q6: 如何实现搜索功能?

A: 可以在外部监听value变化或使用搜索按钮:

@State searchValue:string=''SearchInput({value:$searchValue,showSearchButton:true})// 在 aboutToUpdate 或其他地方处理搜索aboutToUpdate(){if(this.searchValue.trim().length>0){// 执行搜索逻辑this.performSearch(this.searchValue)}}

Q7: 历史记录如何持久化?

A: 可以使用本地存储:

// 保存历史记录localStorage.setItem('searchHistory',JSON.stringify(this.historyItems))// 读取历史记录constsaved=localStorage.getItem('searchHistory')if(saved){this.historyItems=JSON.parse(saved)}

总结

SearchInput 是控件库中专用于搜索场景的输入框组件,具有以下核心特性:

  1. 搜索图标:左侧自动显示搜索图标,直观明了
  2. 清除按钮:支持一键清除输入内容
  3. 搜索按钮:支持显示搜索按钮,明确操作
  4. 历史记录:支持显示搜索历史,快速复用
  5. 易于使用:简单的 API,开箱即用
  6. 主题配置:所有样式都可通过代码配置
  7. 品牌标识:自动包含品牌标识,保持视觉统一

关键要点

  • ✅ 使用$variableName创建双向绑定
  • ✅ 使用showSearchButton显示搜索按钮
  • ✅ 使用showHistoryhistoryItems显示历史记录
  • ✅ 使用showClearButton控制清除按钮显示
  • ✅ 使用inputSize属性选择合适尺寸
  • ✅ 通过ComponentTheme自定义全局样式

适用场景

  • 搜索功能
  • 筛选输入
  • 快速查找
  • 历史复用

下一篇预告:TextArea(多行文本输入)详解


本文档属于《鸿蒙PC UI控件库开发系列文章》第10篇

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/20 17:52:45

AutoGPT在碳排放计算工具开发中的自动化支持

AutoGPT在碳排放计算工具开发中的自动化支持 如今,企业在应对气候变化、履行ESG责任的过程中,碳排放核算早已不再是可有可无的“附加项”,而是关乎合规性、融资能力甚至品牌声誉的关键环节。然而,现实却令人沮丧:大多数…

作者头像 李华
网站建设 2026/5/16 13:01:24

69、Ubuntu与Linux网络资源全攻略

Ubuntu与Linux网络资源全攻略 一、Ubuntu安装与开发参与 Ubuntu的安装方式较为多样。你可以使用标准的Ubuntu CD进行安装,也能从官网下载ISO9660格式(文件名以 .iso 结尾)的镜像文件,然后将其刻录到700MB的CD - R或DVD上。若不想自行下载和刻录,还能通过 http://shipit.…

作者头像 李华
网站建设 2026/5/23 1:27:01

AutoGPT与Google Calendar联动:智能提醒系统构建

AutoGPT与Google Calendar联动:智能提醒系统构建 在信息过载、任务繁杂的现代工作环境中,很多人每天都在“追赶时间”——明明列了待办清单,却总是在最后一刻才开始准备会议;计划好要学习新技能,但总是被临时事务打断。…

作者头像 李华
网站建设 2026/5/23 18:45:07

NVIDIA Llama Nemotro 推理模型构建企业级 AI 智能体

总结分析:使用先进的开放式 NVIDIA Llama Nemotron 推理模型构建企业级 AI 智能体 核心主旨: 本文旨在宣布并详细介绍 NVIDIA 新推出的 Llama Nemotron 推理模型系列。该系列的核心目标是解决企业级 AI 智能体对强大推理能力的迫切需求,通过…

作者头像 李华
网站建设 2026/5/22 19:40:00

使用 Java、Spring Boot 和 Spring AI 开发符合 A2A 标准的 AI 智能体

I 智能体指的是一种软件实体,它能够利用自然语言处理、机器学习或推理系统等人工智能技术,自主感知、推理和行动,以实现特定目标。我为 Telex 开发了一个 AI 智能体,该智能体接收一个正则表达式模式,并就该模式所匹配的…

作者头像 李华