• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Search
2
3搜索框组件,适用于浏览器的搜索内容输入框等应用场景。
4
5> **说明:**
6>
7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## 子组件
10
1112
13## 接口
14
15Search(options?: { value?: string, placeholder?: ResourceStr, icon?: string, controller?: SearchController })
16
17**参数:**
18
19| 参数名      | 参数类型                                             | 必填 | 参数描述                                                     |
20| ----------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
21| value       | string                                               | 否   | 设置当前显示的搜索文本内容。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 |
22| placeholder | [ResourceStr](ts-types.md#resourcestr)<sup>10+</sup> | 否   | 设置无输入时的提示文本。                                     |
23| icon        | string                                               | 否   | 设置搜索图标路径,默认使用系统搜索图标。<br/>**说明:** <br/>icon的数据源支持本地图片和网络图片。<br/>-&nbsp;支持的图片格式包括png、jpg、bmp、svg、gif和pixelmap。<br/>-&nbsp;支持Base64字符串。格式data:image/[png\|jpeg\|bmp\|webp];base64,[base64 data], 其中[base64 data]为Base64字符串数据。<br/>如果与属性searchIcon同时设置,则searchIcon优先。 |
24| controller  | SearchController                                     | 否   | 设置Search组件控制器。                                       |
25
26## 属性
27
28除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
29
30| 名称                    | 参数类型                                         | 描述                                           |
31| ----------------------- | ------------------------------------------------ | ---------------------------------------------- |
32| searchButton<sup>10+</sup> | value: string,<br />option?: [SearchButtonOptions](#searchbuttonoptions10对象说明)            | 搜索框末尾搜索按钮文本内容,默认无搜索按钮。               |
33| placeholderColor        | [ResourceColor](ts-types.md#resourcecolor)       | 设置placeholder文本颜色。<br />默认值:'#99182431'。   |
34| placeholderFont         | [Font](ts-types.md#font)                         | 设置placeholder文本样式,包括字体大小,字体粗细,字体族,字体风格。目前仅支持默认字体族。                         |
35| textFont                | [Font](ts-types.md#font)                         | 设置搜索框内输入文本样式,包括字体大小,字体粗细,字体族,字体风格。目前仅支持默认字体族。                           |
36| textAlign               | [TextAlign](ts-appendix-enums.md#textalign)      | 设置文本在搜索框中的对齐方式。目前支持的对齐方式有:Start、Center、End。<br/>默认值:TextAlign.Start    |
37| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | 设置输入的文本是否可复制。<br />默认值:CopyOptions.LocalDevice,支持设备内复制。<br/>设置CopyOptions.None时,当前Search中的文字无法被复制或剪切,仅支持粘贴。<br/>**说明:**<br/>copyOption对于拖拽,只限制是否选中,不涉及拖拽范围。 |
38| searchIcon<sup>10+</sup>   | [IconOptions](#iconoptions10对象说明)                                                  | 设置左侧搜索图标样式。                                       |
39| cancelButton<sup>10+</sup> | {<br/>style? : [CancelButtonStyle](#cancelbuttonstyle10枚举说明)<br/>icon?: [IconOptions](#iconoptions10对象说明) <br/>} | 设置右侧清除按钮样式。<br />默认值:<br />{<br />style:CancelButtonStyle.INPUT<br />} |
40| fontColor<sup>10+</sup>    | [ResourceColor](ts-types.md#resourcecolor)                   | 设置输入文本的字体颜色。<br />默认值:'#FF182431'。<br />**说明:**<br />[文本通用属性](ts-universal-attributes-text-style.md)fontSize、fontStyle、fontWeight和fontFamily在textFont属性中设置。 |
41| caretStyle<sup>10+</sup>  | [CaretStyle](#caretstyle10对象说明)                                                  | 设置光标样式。<br />默认值:<br />{<br />width: '1.5vp',<br />color: '#007DFF'<br />} |
42| enableKeyboardOnFocus<sup>10+</sup> | boolean | Search获焦时,是否绑定输入法<br/>默认值:true。从API version 10开始,获焦默认绑定输入法。 |
43| selectionMenuHidden<sup>10+</sup> | boolean | 设置长按输入框或者右键输入框时,是否弹出文本选择菜单。<br />默认值:false |
44| customKeyboard<sup>10+</sup> | [CustomBuilder](ts-types.md#custombuilder8) | 设置自定义键盘。<br/>**说明:**<br/>当设置自定义键盘时,输入框激活后不会打开系统输入法,而是加载指定的自定义组件。<br/>自定义键盘的高度可以通过自定义组件根节点的height属性设置,宽度不可设置,使用系统默认值。<br/>自定义键盘采用覆盖原始界面的方式呈现,不会对应用原始界面产生压缩或者上提。<br/>自定义键盘无法获取焦点,但是会拦截手势事件。<br/>默认在输入控件失去焦点时,关闭自定义键盘,开发者也可以通过[stopEditing](#stopediting10)方法控制键盘关闭。 |
45
46## IconOptions<sup>10+</sup>对象说明
47
48| 参数名 | 参数类型                                   | 必填 | 参数描述    |
49| ------ | ------------------------------------------ | ---- | ----------- |
50| size   | [Length](ts-types.md#length)               | 否   | 图标尺寸,不支持百分比。    |
51| color  | [ResourceColor](ts-types.md#resourcecolor) | 否   | 图标颜色。    |
52| src    | [ResourceStr](ts-types.md#resourcestr)     | 否   | 图标/图片源。 |
53
54## CaretStyle<sup>10+</sup>对象说明
55
56| 参数名 | 参数类型                                   | 必填 | 参数描述 |
57| ------ | ------------------------------------------ | ---- | -------- |
58| width  | [Length](ts-types.md#length)               | 否   | 光标尺寸,不支持百分比。 |
59| color  | [ResourceColor](ts-types.md#resourcecolor) | 否   | 光标颜色。 |
60
61## SearchButtonOptions<sup>10+</sup>对象说明
62
63| 参数名    | 参数类型                                   | 必填 | 参数描述         |
64| --------- | ------------------------------------------ | ---- | ---------------- |
65| fontSize  | [Length](ts-types.md#length)               | 否   | 文本按钮字体大小,不支持百分比。 |
66| fontColor | [ResourceColor](ts-types.md#resourcecolor) | 否   | 文本按钮字体颜色。 |
67
68## CancelButtonStyle<sup>10+</sup>枚举说明
69
70| 名称                    | 描述             |
71| ----------------------- | ---------------- |
72| CONSTANT  | 清除按钮常显样式。 |
73| INVISIBLE | 清除按钮常隐样式。 |
74| INPUT     | 清除按钮输入样式。 |
75
76## 事件
77
78除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:
79
80| 名称                                                         | 功能描述                                                     |
81| ------------------------------------------------------------ | ------------------------------------------------------------ |
82| onSubmit(callback: (value: string) => void)                  | 点击搜索图标、搜索按钮或者按下软键盘搜索按钮时触发该回调。<br> -value: 当前搜索框中输入的文本内容。 |
83| onChange(callback: (value: string) => void)                  | 输入内容发生变化时,触发该回调。<br> -value: 当前搜索框中输入的文本内容。 |
84| onCopy(callback: (value: string) => void)                    | 长按搜索框弹出剪切板之后,点击剪切板的复制按钮触发该回调。<br> -value: 复制的文本内容。 |
85| onCut(callback: (value: string) => void)                     | 长按搜索框弹出剪切板之后,点击剪切板的剪切按钮触发该回调。<br> -value: 剪切的文本内容。 |
86| onPaste(callback: (value: string) => void)                   | 长按搜索框弹出剪切板之后,点击剪切板的粘贴按钮触发该回调。<br> -value: 粘贴的文本内容。 |
87| onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void)<sup>10+</sup> | 文本选择的位置发生变化时,触发该回调。<br />selectionStart:文本选择区域起始位置,文本框中文字的起始位置为0。<br />selectionEnd:文本选择区域结束位置。 |
88| onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void)<sup>10+</sup> | 文本内容滚动时,触发该回调。<br />totalOffsetX:文本在内容区的横坐标偏移,单位px。<br />totalOffsetY:文本在内容区的纵坐标偏移,单位px。 |
89
90## SearchController
91
92Search组件的控制器,目前通过它可控制Search组件的光标位置。
93
94### 导入对象
95```
96controller: SearchController = new SearchController()
97```
98### caretPosition
99
100caretPosition(value: number): void
101
102设置输入光标的位置。
103
104**参数:**
105
106| 参数名 | 参数类型 | 必填 | 参数描述                           |
107| ------ | -------- | ---- | ---------------------------------- |
108| value  | number   | 是   | 从字符串开始到光标所在位置的长度。 |
109
110### stopEditing<sup>10+</sup>
111
112stopEditing(): void
113
114退出编辑态。
115
116### getTextContentRect<sup>10+</sup>
117
118getTextContentRect(): RectResult
119
120获取已编辑文本内容区域相对组件的位置和大小,返回值单位为像素。
121
122**返回值:**
123
124| 类型       | 说明       |
125| -------------------  | -------- |
126| [RectResult](#rectresult10) | 已编辑文本内容的相对组件的位置和大小。 |
127
128> **说明:**
129>
130> - 返回的位置信息是相对Search组件中搜索图标的偏移值。
131> - 不输入文本时,返回值中有相对Search组件的位置信息,但大小为0。
132> - 返回值中的位置信息是第一个字符相对于可编辑组件的位置。
133
134### RectResult<sup>10+</sup>
135
136位置和大小,单位均为像素。
137
138| 参数      | 类型     | 描述 |
139| ------- | ------ | ----------------------- |
140| x     | number | 水平方向横坐标。|
141| y     | number | 竖直方向纵坐标。|
142| width | number | 内容宽度大小。|
143| height | number | 内容高度大小。|
144
145
146### getTextContentLineCount<sup>10+</sup>
147
148getTextContentLineCount(): number
149
150获取已编辑文本内容的行数。
151
152**返回值:**
153
154| 类型  | 说明       |
155| ----- | -------- |
156| number| 已编辑文本内容行数。 |
157
158##  示例
159
160### 示例1
161
162```ts
163// xxx.ets
164@Entry
165@Component
166struct SearchExample {
167  @State changeValue: string = ''
168  @State submitValue: string = ''
169  controller: SearchController = new SearchController()
170
171  build() {
172    Column() {
173      Text('onSubmit:' + this.submitValue).fontSize(18).margin(15)
174      Text('onChange:' + this.changeValue).fontSize(18).margin(15)
175      Search({ value: this.changeValue, placeholder: 'Type to search...', controller: this.controller })
176        .searchButton('SEARCH')
177        .width('95%')
178        .height(40)
179        .backgroundColor('#F5F5F5')
180        .placeholderColor(Color.Grey)
181        .placeholderFont({ size: 14, weight: 400 })
182        .textFont({ size: 14, weight: 400 })
183        .onSubmit((value: string) => {
184          this.submitValue = value
185        })
186        .onChange((value: string) => {
187          this.changeValue = value
188        })
189        .margin(20)
190      Button('Set caretPosition 1')
191        .onClick(() => {
192          // 设置光标位置到输入的第一个字符后
193          this.controller.caretPosition(1)
194        })
195    }.width('100%')
196  }
197}
198```
199
200![search](figures/search.gif)
201
202### 示例2
203
204```ts
205// xxx.ets
206@Entry
207@Component
208struct SearchExample {
209  @State changeValue: string = ''
210  @State submitValue: string = ''
211
212  build() {
213    Column() {
214      Text('onSubmit:' + this.submitValue).fontSize(18).margin(15)
215      Search({ value: this.changeValue, placeholder: 'Type to search...' })
216        .searchButton('SEARCH')
217        .searchIcon({
218          src: $r('app.media.search')
219        })
220        .cancelButton({
221          style: CancelButtonStyle.CONSTANT,
222          icon: {
223            src: $r('app.media.cancel')
224          }
225        })
226        .width('90%')
227        .height(40)
228        .backgroundColor('#F5F5F5')
229        .placeholderColor(Color.Grey)
230        .placeholderFont({ size: 14, weight: 400 })
231        .textFont({ size: 14, weight: 400 })
232        .onSubmit((value: string) => {
233          this.submitValue = value
234        })
235        .onChange((value: string) => {
236          this.changeValue = value
237        })
238        .margin(20)
239    }.width('100%')
240  }
241}
242```
243
244![searchButton](figures/searchButton.gif)
245
246
247### 示例3
248
249```ts
250// xxx.ets
251@Entry
252@Component
253struct SearchExample {
254  controller: SearchController = new SearchController()
255  @State inputValue: string = ""
256
257  // 自定义键盘组件
258  @Builder CustomKeyboardBuilder() {
259    Column() {
260      Button('x').onClick(() => {
261        // 关闭自定义键盘
262        this.controller.stopEditing()
263      })
264      Grid() {
265        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {
266          GridItem() {
267            Button(item + "")
268              .width(110).onClick(() => {
269              this.inputValue += item
270            })
271          }
272        })
273      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5)
274    }.backgroundColor(Color.Gray)
275  }
276
277  build() {
278    Column() {
279      Search({ controller: this.controller, value: this.inputValue})
280        // 绑定自定义键盘
281        .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 })
282    }
283  }
284}
285````
286
287![customKeyboard](figures/searchCustomKeyboard.png)