# search开发指导 提供搜索框组件,用于提供用户搜索内容的输入区域,具体用法请参考[search](../reference/arkui-js/js-components-basic-search.md)。 ## 创建search组件 在pages/index目录下的hml文件中创建一个search组件。 ```html
``` ```css /* xxx.css */ .container { width: 100%; height: 100%; flex-direction: column; align-items: center; justify-content: center; background-color: #F1F3F5; } ``` ![zh-cn_image_0000001218760480](figures/zh-cn_image_0000001218760480.png) ## 设置属性 通过设置hint、icon和searchbutton属性设置搜索框的提示文字、图标和末尾搜索按钮的内容。 ```html
``` ```css /* xxx.css */ .container { width: 100%; height: 100%; flex-direction: column; align-items: center; justify-content: center; background-color: #F1F3F5; } ``` ![zh-cn_image_0000001183438596](figures/zh-cn_image_0000001183438596.png) ## 添加样式 通过color、placeholder-color和caret-color样式来设置搜索框的文本颜色、提示文本颜色和光标颜色。 ```html
``` ```css /* xxx.css */ .container { width: 100%; height: 100%; flex-direction: column; align-items: center; justify-content: center; background-color: #F1F3F5; } search{ color: black; placeholder-color: black; caret-color: red; } ``` ![zh-cn_image_0000001228920921](figures/zh-cn_image_0000001228920921.gif) ## 绑定事件 向search组件添加change、search、submit、share和translate事件,对输入信息进行操作。 ```html
Enter text and then touch and hold what you've entered
``` ```css /* xxx.css */ .container { width: 100%; height: 100%; flex-direction: column; align-items: center; justify-content: center; background-color: #F1F3F5; } text{ width: 100%; font-size: 25px; text-align: center; margin-bottom: 100px; } ``` ```js // index.js import promptAction from '@ohos.promptAction' export default { search(e){ promptAction.showToast({ message: e.value, duration: 3000, }); }, translate(e){ promptAction.showToast({ message: e.value, duration: 3000, }); }, share(e){ promptAction.showToast({ message: e.value, duration: 3000, }); }, change(e){ promptAction.showToast({ message: e.value, duration: 3000, }); }, submit(e){ promptAction.showToast({ message: 'submit', duration: 3000, }); } } ``` ![zh-cn_image_0000001182187434](figures/zh-cn_image_0000001182187434.gif) ## 场景示例 在本场景中通过下拉菜单选择search、Textarea和Input组件来实现搜索和输入效果。 ```html
``` ```css /* xxx.css */ .field { width: 80%; color: mediumaquamarine; font-weight: 600; placeholder-color: orangered; } .slt1{ font-size: 50px; position: absolute; left: 50px; top: 50px; } ``` ```js // index.js import promptAction from '@ohos.promptAction'; export default { data: { showsearch: true, showtextarea: false, showinput: false, showsec: true, }, setfield(e) { this.field = e.newValue if (e.newValue == 'search') { this.showsearch = true this.showtextarea = false this.showinput = false } else if (e.newValue == 'textarea') { this.showsearch = false this.showtextarea = true this.showinput = false } else { this.showsearch = false this.showtextarea = false this.showinput = true } }, submit(e) { promptAction.showToast({ message: '搜索!', duration: 2000 }) }, change(e) { promptAction.showToast({ message: '内容:' + e.text, duration: 2000 }) } } ``` ![zh-cn_image_0000001183283966](figures/zh-cn_image_0000001183283966.gif)