js-apis-inputmethod.md 33.7 KB
Newer Older
Z
zhouyongfei 已提交
1 2
# 输入法框架

T
explain  
tianyu 已提交
3 4
本模块提供对输入法框架的管理,包括隐藏输入法、查询已安装的输入法列表和显示输入法选择对话框。

5
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
6
>
Z
zhouyongfei 已提交
7 8 9 10 11 12
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

```
13
import inputMethod from '@ohos.inputmethod';
Z
zhouyongfei 已提交
14 15 16 17 18 19
```

## inputMethod<sup>8+</sup>

常量值。

20
**系统能力**:以下各项对应的系统能力均为SystemCapability.MiscServices.InputMethodFramework
Z
刷新  
zhouyongfei 已提交
21

Z
zhouyongfei 已提交
22 23
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
Z
刷新  
zhouyongfei 已提交
24
| MAX_TYPE_NUM | number | 是 | 否 | 可支持的最大输入法个数。 |
Z
zhouyongfei 已提交
25 26


27
## InputMethodProperty<sup>8+</sup>
Z
zhouyongfei 已提交
28 29 30

输入法应用属性。

31
**系统能力**:以下各项对应的系统能力均为SystemCapability.MiscServices.InputMethodFramework
Z
刷新  
zhouyongfei 已提交
32

Z
zhouyongfei 已提交
33 34
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
35 36
| packageName<sup>deprecated</sup> | string | 是 | 否 | 包名。 |
| methodId<sup>deprecated</sup> | string | 是 | 否 | Ability名。 |
H
Hollokin 已提交
37 38 39 40 41 42
| name<sup>9+</sup>  | string | 是 | 否 | 包名,非必填项。 |
| id<sup>9+</sup>    | string | 是 | 否 | Ability名,非必填项。 |
| label<sup>9+</sup>    | string | 是 | 否 | 输入法标签,非必填项。| 
| icon<sup>9+</sup>    | string | 是 | 否 | 输入法图标,非必填项。 |
| iconId<sup>9+</sup>    | number | 是 | 否 | 输入法图标id,非必填项。 |
| extra<sup>9+</sup>    | object | 是 | 否 | 输入法其他信息,非必填项。 |
Z
zhouyongfei 已提交
43

44
## inputMethod.getInputMethodController<sup>deprecated</sup>
Z
zhouyongfei 已提交
45 46 47

getInputMethodController(): InputMethodController

48
获取客户端实例[InputMethodController](#inputmethodcontroller)
Z
zhouyongfei 已提交
49

50 51 52 53 54
> **说明:** 
> 从API version 9开始废弃, 建议使用[getController](#getController)替代
>
> 从 API version 6开始支持。

55
**系统能力**:SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
56

57
**返回值:**
Z
zhouyongfei 已提交
58

59 60 61
| 类型                                            | 说明                     |
| ----------------------------------------------- | ------------------------ |
| [InputMethodController](#inputmethodcontroller) | 回调返回当前客户端实例。 |
Z
zhouyongfei 已提交
62

63
**示例:**
T
explain  
tianyu 已提交
64

65
```js
Z
zhouyongfei 已提交
66
  var InputMethodController = inputMethod.getInputMethodController();
67
```
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
## inputMethod.getController<sup>9+</sup>

getController(): InputMethodController

获取客户端实例[InputMethodController](#inputmethodcontroller)

**系统能力**:SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型                                            | 说明                     |
| ----------------------------------------------- | ------------------------ |
| [InputMethodController](#inputmethodcontroller) | 回调返回当前客户端实例。 |

**示例:**

```js
  var InputMethodController = inputMethod.getController();
```

H
Hollokin 已提交
89
## inputMethod.getInputMethodSetting<sup>deprecated</sup>
Z
zhouyongfei 已提交
90 91 92

getInputMethodSetting(): InputMethodSetting

93
获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)
Z
zhouyongfei 已提交
94

H
Hollokin 已提交
95 96 97 98 99
> **说明:** 
> 从API version 9开始废弃, 建议使用[getSetting](#getSetting)替代
>
> 从 API version 6开始支持。

100
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
101

102
**返回值:**
Z
zhouyongfei 已提交
103

T
explain  
tianyu 已提交
104 105
| 类型                                      | 说明                         |
| ----------------------------------------- | ---------------------------- |
106
| [InputMethodSetting](#inputmethodsetting8) | 回调返回当前客户端设置实例。 |
Z
zhouyongfei 已提交
107 108


109
**示例:**
110 111

```js
Z
zhouyongfei 已提交
112
  var InputMethodSetting = inputMethod.getInputMethodSetting();
113
```
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

## inputMethod.getSetting<sup>9+</sup>

getSetting(): InputMethodSetting

获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型                                      | 说明                         |
| ----------------------------------------- | ---------------------------- |
| [InputMethodSetting](#inputmethodsetting8) | 回调返回当前客户端设置实例。 |


**示例:**

```js
  var InputMethodSetting = inputMethod.getSetting();
```

G
gaoxiang 已提交
136
## inputMethod.switchInputMethod<sup>9+</sup>
Z
zhouyongfei 已提交
137

Z
zhaolinglan 已提交
138
switchInputMethod(target: InputMethodProperty, callback: AsyncCallback&lt;boolean&gt;): void
G
gaoxiang 已提交
139

140
切换输入法。此接口仅可在Stage模型下使用。使用callback形式返回结果。参数个数为2,否则抛出异常。
141

142 143
**需要权限**: ohos.permission.CONNECT_IME_ABILITY

Z
zhaolinglan 已提交
144
**系统能力**:SystemCapability.MiscServices.InputMethodFramework
G
gaoxiang 已提交
145 146 147 148 149

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
G
gaoxiang 已提交
150
  |target | [InputmethodProperty](#inputmethodproperty8) | 是 | 传入要切换的目标输入法。 |
G
gaoxiang 已提交
151 152 153 154 155 156
  | callback | AsyncCallback&lt;boolean&gt; | 是 | 返回输入法切换是否成功。 |


**示例:**

```js
H
Hollokin 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
try{
    inputMethod.switchInputMethod({packageName:'com.example.kikakeyboard', methodId:'com.example.kikakeyboard'}, (err, result) => {
        if (err) {
            // 处理业务逻辑执行错误
            console.error('switchInputMethod err: ' + JSON.stringify(err));
            return;
        }
        if (result) {
            console.info('Success to switchInputMethod.(callback)');
        } else {
            console.error('Failed to switchInputMethod.(callback)');
        }
    });
} catch(err) {
    // 捕获参数错误
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
G
gaoxiang 已提交
174
```
G
gaoxiang 已提交
175
## inputMethod.switchInputMethod<sup>9+</sup>
Z
zhaolinglan 已提交
176
switchInputMethod(target: InputMethodProperty): Promise&lt;boolean&gt;
G
gaoxiang 已提交
177

178
切换输入法。此接口仅可在Stage模型下使用。使用promise形式返回结果。参数个数为1,否则抛出异常。
G
gaoxiang 已提交
179

180 181
**需要权限**: ohos.permission.CONNECT_IME_ABILITY

Z
zhaolinglan 已提交
182
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
G
gaoxiang 已提交
183

G
gaoxiang 已提交
184
**参数:**
G
gaoxiang 已提交
185 186 187

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
G
gaoxiang 已提交
188
  |target |  [InputmethodProperty](#inputmethodproperty8)| 是 | 传入要切换的目标输入法。 |
G
gaoxiang 已提交
189

G
gaoxiang 已提交
190
**返回值:**
B
bmeangel 已提交
191

G
gaoxiang 已提交
192 193
  | 类型                                      | 说明                         |
  | ----------------------------------------- | ---------------------------- |
194
  | Promise\<boolean> | 回调返回切换后的输入法。 |
G
gaoxiang 已提交
195

G
gaoxiang 已提交
196 197 198
**示例:**

```js
H
Hollokin 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
try {
    inputMethod.switchInputMethod({packageName:'com.example.kikakeyboard', methodId:'com.example.kikakeyboard'}).then((result) => {
        if (result) {
            console.info('Success to switchInputMethod.(promise)');
        } else {
            console.error('Failed to switchInputMethod.(promise)');
        }
    }).catch((err) => {
        console.error('switchInputMethod promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
G
gaoxiang 已提交
212
```
213

Z
zhaolinglan 已提交
214 215 216 217
## inputMethod.getCurrentInputMethod<sup>9+</sup>

getCurrentInputMethod(): InputMethodProperty

Z
zhaolinglan 已提交
218
获取当前输入法扩展应用,提供同步接口,返回当前输入法属性对象。
Z
zhaolinglan 已提交
219 220 221 222 223

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

Z
zhaolinglan 已提交
224 225 226
| 类型                                         | 说明                     |
| -------------------------------------------- | ------------------------ |
| [InputmethodProperty](#inputmethodproperty8) | 返回当前输入法属性对象。 |
Z
zhaolinglan 已提交
227 228 229 230 231 232 233

**示例:**

```js
var currentIme = inputMethod.getCurrentInputMethod();
```

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup>

switchCurrentInputMethodSubtype(target: InputMethodSubtype, callback: AsyncCallback<boolean>): void

选择当前的输入法子类型。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
|target |  [InputMethodSubtype](#InputMethodSubtype)| 是 | 传入要切换的目标输入法子类型。 |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 返回输入法子类型切换是否成功。 |

**示例:**

```js
H
Hollokin 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
try {
    inputMethod.switchCurrentInputMethodSubtype(inputMethodSubProperty, (err, result) => {
        if (err) {
            console.error('switchCurrentInputMethodSubtype err: ' + JSON.stringify(err));
            return;
        }
        if (result) {
            console.info('Success to switchCurrentInputMethodSubtype.(callback)');
        } else {
            console.error('Failed to switchCurrentInputMethodSubtype.(callback)');
        }
    });
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
```

## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup>

switchCurrentInputMethodSubtype(target: InputMethodSubtype): Promise&lt;boolean&gt;

切换输入法子类型。此接口仅可在Stage模型下使用。使用promise形式返回结果。参数个数为1,否则抛出异常。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
|target |  [InputMethodSubtype](#InputMethodSubtype)| 是 | 传入要切换的目标输入法子类型。 |

**示例:**

```js
H
Hollokin 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
try {
    inputMethod.switchCurrentInputMethodSubtype(inputMethodSubProperty).then((result) => {
        if (result) {
            console.info('Success to switchCurrentInputMethodSubtype.(promise)');
        } else {
            console.error('Failed to switchCurrentInputMethodSubtype.(promise)');
        }
    }).catch((err) => {
        console.error('switchCurrentInputMethodSubtype promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
```

## inputMethod.getCurrentInputMethodSubtype<sup>9+</sup>

getCurrentInputMethodSubtype(): InputMethodSubtype

获取当前输入法子类型。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型                                         | 说明                     |
| -------------------------------------------- | ------------------------ |
| [InputMethodSubtype](#InputMethodSubtype) | 返回当前输入法子类型对象。 |

**示例:**

```js
var currentImeSubType = inputMethod.getCurrentInputMethodSubtype();
```

## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup>

switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, callback: AsyncCallback<boolean>): void

选择输入法和子类型。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
|inputMethodProperty |  [InputMethodProperty](#InputMethodProperty)| 是 | 传入要切换的目标输入法。 |
|inputMethodSubtype |  [inputMethodSubtype](#InputMethodSubtype)| 是 | 传入要切换的目标输入法子类型。 |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 返回输入法和子类型切换是否成功。 |

**示例:**

```js
H
Hollokin 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
let inputMethodProperty = {
    name: 'com.example.kikakeyboard', 
    id: 'com.example.kikakeyboard'
}
try {
    inputMethod.switchCurrentInputMethodAndSubtype(inputMethodProperty, inputMethodSubProperty, (err,result) => {
        if (err) {
            console.error('switchCurrentInputMethodAndSubtype err: ' + JSON.stringify(err));
            return;
        }
        if (result) {
            console.info('Success to switchCurrentInputMethodAndSubtype.(callback)');
        } else {
            console.error('Failed to switchCurrentInputMethodAndSubtype.(callback)');
        }
    });
} catch (err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
```

## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup>

switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, ): Promise&lt;boolean&gt;

切换输入法子类型。此接口仅可在Stage模型下使用。使用promise形式返回结果。参数个数为1,否则抛出异常。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
|inputMethodProperty |  [InputMethodProperty](#InputMethodProperty)| 是 | 传入要切换的目标输入法。 |
|inputMethodSubtype |  [inputMethodSubtype](#InputMethodSubtype)| 是 | 传入要切换的目标输入法子类型。 |

**示例:**

```js
H
Hollokin 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
let inputMethodProperty = {
    name: 'com.example.kikakeyboard', 
    id: 'com.example.kikakeyboard'
}
try {
    inputMethod.switchCurrentInputMethodAndSubtype(property, subType).then((result) => {
        if (result) {
            console.info('Success to switchCurrentInputMethodAndSubtype.(promise)');
        } else {
            console.error('Failed to switchCurrentInputMethodAndSubtype.(promise)');
        }
    }).catch((err) => {
        console.error('switchCurrentInputMethodAndSubtype promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
422 423
```

424
## InputMethodController
Z
zhouyongfei 已提交
425

H
Hollokin 已提交
426
下列API示例中都需使用[getController](#inputmethodgetcontroller)回调获取到InputMethodController实例,再通过此实例调用对应方法。
Z
zhouyongfei 已提交
427

428
### stopInput<sup>deprecated</sup>
Z
zhouyongfei 已提交
429 430 431

stopInput(callback: AsyncCallback&lt;boolean&gt;): void

432
隐藏输入法。使用callback形式返回结果。参数个数为1,否则抛出异常。
Z
zhouyongfei 已提交
433

434
**系统能力**:SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
435

436
**参数:**
Z
zhouyongfei 已提交
437

T
explain  
tianyu 已提交
438 439 440
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 返回输入法隐藏是否成功。 |
Z
zhouyongfei 已提交
441

442
**示例:**
Z
zhouyongfei 已提交
443

444
```js
Z
zhaolinglan 已提交
445
InputMethodController.stopInput((error, result) => {
Z
zhaolinglan 已提交
446
    if (error) {
Z
zhaolinglan 已提交
447
        console.error('failed to stopInput because: ' + JSON.stringify(error));
448 449
        return;
    }
Z
zhaolinglan 已提交
450
    if (result) {
Z
zhaolinglan 已提交
451
        console.info('Success to stopInput.(callback)');
Z
zhaolinglan 已提交
452
    } else {
Z
zhaolinglan 已提交
453
        console.error('Failed to stopInput.(callback)');
Z
zhaolinglan 已提交
454
    }
455
});
Z
zhouyongfei 已提交
456 457
```

458
### stopInput<sup>deprecated</sup>
Z
zhouyongfei 已提交
459 460 461

stopInput(): Promise&lt;boolean&gt;

462
隐藏输入法。使用promise形式返回结果。参数个数为0,否则抛出异常。
Z
zhouyongfei 已提交
463

464
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
465

466
**返回值:**
Z
zhouyongfei 已提交
467

T
explain  
tianyu 已提交
468 469 470
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;boolean&gt; | 返回输入法隐藏是否成功。 |
Z
zhouyongfei 已提交
471

472
**示例:**
Z
zhouyongfei 已提交
473

474
```js
Z
zhaolinglan 已提交
475 476
InputMethodController.stopInput().then((result) => {
    if (result) {
Z
zhaolinglan 已提交
477
        console.info('Success to stopInput.(promise)');
Z
zhaolinglan 已提交
478
    } else {
Z
zhaolinglan 已提交
479
        console.error('Failed to stopInput.(promise)');
Z
zhaolinglan 已提交
480 481
    }
}).catch((err) => {
Z
zhaolinglan 已提交
482
    console.error('stopInput promise err: ' + err);
Z
zhaolinglan 已提交
483
})
Z
zhouyongfei 已提交
484 485
```

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
### stopInputSession<sup>9+</sup>

stopInputSession(callback: AsyncCallback&lt;boolean&gt;): void

隐藏输入法。使用callback形式返回结果。参数个数为1,否则抛出异常。

**系统能力**:SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 返回输入法隐藏是否成功。 |

**示例:**

```js
H
Hollokin 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
try {
    InputMethodController.stopInputSession((error, result) => {
        if (error) {
            console.error('failed to stopInputSession because: ' + JSON.stringify(error));
            return;
        }
        if (result) {
            console.info('Success to stopInputSession.(callback)');
        } else {
            console.error('Failed to stopInputSession.(callback)');
        }
    });
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
```

### stopInputSession

stopInputSession(): Promise&lt;boolean&gt;

隐藏输入法。使用promise形式返回结果。参数个数为0,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;boolean&gt; | 返回输入法隐藏是否成功。 |

**示例:**

```js
H
Hollokin 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549
try {
    InputMethodController.stopInputSession().then((result) => {
        if (result) {
            console.info('Success to stopInputSession.(promise)');
        } else {
            console.error('Failed to stopInputSession.(promise)');
        }
    }).catch((err) => {
        console.error('stopInputSession promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
550 551
```

Z
zhaolinglan 已提交
552
### showSoftKeyboard<sup>9+</sup> ###
Z
zhaolinglan 已提交
553

Z
zhaolinglan 已提交
554
showSoftKeyboard(callback: AsyncCallback&lt;void&gt;): void
Z
zhaolinglan 已提交
555

556
显示软键盘,使用callback异步回调。参数个数为1,否则抛出异常。
Z
zhaolinglan 已提交
557

Z
zhaolinglan 已提交
558
**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
Z
zhaolinglan 已提交
559 560 561

**参数:**

Z
zhaolinglan 已提交
562
| 参数名   | 参数类型                  | 必填 | 说明       |
Z
zhaolinglan 已提交
563 564
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。 |
Z
zhaolinglan 已提交
565 566 567 568

**示例:**

```js
Z
zhaolinglan 已提交
569
InputMethodController.showSoftKeyboard((err) => {
Z
zhaolinglan 已提交
570
    if (err === undefined) {
Z
zhaolinglan 已提交
571
        console.info('showSoftKeyboard success');
Z
zhaolinglan 已提交
572
    } else {
Z
zhaolinglan 已提交
573
        console.error('showSoftKeyboard failed because : ' + JSON.stringify(err));
Z
zhaolinglan 已提交
574 575 576 577
    }
})
```

Z
zhaolinglan 已提交
578
### showSoftKeyboard<sup>9+</sup> ###
Z
zhaolinglan 已提交
579

Z
zhaolinglan 已提交
580
showSoftKeyboard(): Promise&lt;void&gt;
Z
zhaolinglan 已提交
581

582
显示软键盘,使用Promise异步回调。参数个数为0,否则抛出异常。
Z
zhaolinglan 已提交
583

Z
zhaolinglan 已提交
584
**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
Z
zhaolinglan 已提交
585 586 587

**返回值:**

Z
zhaolinglan 已提交
588 589 590
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
Z
zhaolinglan 已提交
591 592 593 594

**示例:**

```js
Z
zhaolinglan 已提交
595 596 597
InputMethodController.showSoftKeyboard().then(async (err) => {
    console.log('showSoftKeyboard success');
}).catch((err) => {
Z
zhaolinglan 已提交
598
    console.error('showSoftKeyboard promise err: ' + JSON.stringify(err));
Z
zhaolinglan 已提交
599 600 601
});
```

Z
zhaolinglan 已提交
602
### hideSoftKeyboard<sup>9+</sup> ###
Z
zhaolinglan 已提交
603

Z
zhaolinglan 已提交
604
hideSoftKeyboard(callback: AsyncCallback&lt;void&gt;): void
Z
zhaolinglan 已提交
605

606
隐藏软键盘,使用callback异步回调。参数个数为1,否则抛出异常。
Z
zhaolinglan 已提交
607

Z
zhaolinglan 已提交
608
**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
Z
zhaolinglan 已提交
609 610 611

**参数:**

Z
zhaolinglan 已提交
612 613 614
| 参数名   | 参数类型                  | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。 |
Z
zhaolinglan 已提交
615 616 617 618

**示例:**

```js
Z
zhaolinglan 已提交
619
InputMethodController.hideSoftKeyboard((err) => {
Z
zhaolinglan 已提交
620
    if (err === undefined) {
Z
zhaolinglan 已提交
621
        console.info('hideSoftKeyboard success');
Z
zhaolinglan 已提交
622
    } else {
Z
zhaolinglan 已提交
623
        console.error('hideSoftKeyboard failed because : ' + JSON.stringify(err));
Z
zhaolinglan 已提交
624 625 626 627
    }
})
```

Z
zhaolinglan 已提交
628

Z
zhaolinglan 已提交
629
### hideSoftKeyboard<sup>9+</sup> ###
Z
zhaolinglan 已提交
630

Z
zhaolinglan 已提交
631
hideSoftKeyboard(): Promise&lt;void&gt;
Z
zhaolinglan 已提交
632

633
隐藏软键盘,使用Promise异步回调。参数个数为0,否则抛出异常。
Z
zhaolinglan 已提交
634

Z
zhaolinglan 已提交
635
**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
Z
zhaolinglan 已提交
636 637 638

**返回值:**

Z
zhaolinglan 已提交
639 640 641
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
Z
zhaolinglan 已提交
642 643 644 645

**示例:**

```js
Z
zhaolinglan 已提交
646 647 648
InputMethodController.hideSoftKeyboard().then(async (err) => {
    console.log('hideSoftKeyboard success');
}).catch((err) => {
Z
zhaolinglan 已提交
649
    console.error('hideSoftKeyboard promise err: ' + JSON.stringify(err));
Z
zhaolinglan 已提交
650 651 652
});
```

653
## InputMethodSetting<sup>8+</sup>
Z
zhouyongfei 已提交
654

H
Hollokin 已提交
655
下列API示例中都需使用[getSetting](#inputmethodgetsetting)回调获取到InputMethodSetting实例,再通过此实例调用对应方法。
Z
zhouyongfei 已提交
656

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
### on('imeChange')<a name="imeChange"></a>

on(type: 'imeChange', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void

订阅输入法及子类型变化监听事件,使用callback回调返回变化了的输入法及子类型的相关实例。参数个数为3,参数1和参数2为napi_object,参数3为napi_function,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名   | 类型                            | 必填 | 说明                                                         |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                        | 是   | 设置监听类型。<br/>-type为‘imeChange’时表示订阅输入法及子类型变化监听事件。 |
| callback | [inputMethodProperty](#InputMethodProperty), [inputMethodSubtype](#InputMethodSubtype) | 是 | 回调返回输入法及子类型相关实例。 |

**示例:**

  ```js
  InputMethodEngine.on('imeChange', (inputMethodProperty, inputMethodSubtype) => {
      InputMethodProperty = inputMethodProperty;
      InputMethodSubtype = inputMethodSubtype;
  });
  ```

### off('imeChange')<a name="imeChange"></a>

on(type: 'imeChange', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void

取消订阅输入法及子类型变化监听事件,使用callback回调返回取消订阅的输入法及子类型的相关实例。必选参数个数为1,参数1为string,可选参数2为napi_function,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名   | 类型                            | 必填 | 说明                                                         |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                        | 是   | 设置监听类型。<br/>-type为‘imeChange’时表示取消订阅输入法及子类型变化监听事件。 |
| callback | [inputMethodProperty](#InputMethodProperty), [inputMethodSubtype](#InputMethodSubtype) | 否 | 回调返回输入法及子类型相关实例。 |

**示例:**

  ```js
  InputMethodAbility.off('imeChange');
  ```

### listInputMethodSubtype<sup>9+</sup>

listInputMethodSubtype(inputMethodProperty: InputMethodProperty, callback: AsyncCallback&lt;Array&lt;InputMethodSubtype&gt;&gt;): void

查询已安装的输入法子类型列表。使用callback形式返回结果。参数个数为2,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名   | 类型                                               | 必填 | 说明                   |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
| inputMethodProperty | InputMethodProperty| 是 | 要查询的输入法子类型的输入法属性
| callback | Array<[InputMethodSubtype](#InputMethodSubtype)> | 是   | 返回已安装输入法列表。 |

**示例:**

```js
H
Hollokin 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
try {
    InputMethodSetting.listInputMethodSubtype(inputMethodSubProperty, (err,data) => {
        if (err) {
            console.error('listInputMethodSubtype failed because: ' + JSON.stringify(err));
            return;
        }
        console.log('listInputMethodSubtype success');
    });
} catch (err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
```

### listInputMethodSubtype<sup>9+</sup>

listInputMethodSubtype(inputMethodProperty: InputMethodProperty): Promise&lt;Array&lt;InputMethodSubtype&gt;&gt;

查询已安装的满足条件的输入法子类型列表。使用promise形式返回结果。参数个数为1,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型                                                        | 说明                   |
| ----------------------------------------------------------- | ---------------------- |
| Promise<Array<[InputMethodSubtype](#InputMethodSubtype)>> | 返回已安装输入法子类型列表。 |

**示例:**

```js
H
Hollokin 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766
let inputMethodSubProperty = {
    id: "com.example.kikainput",
    label: "ServiceExtAbility"
}
try {
    InputMethodSetting.listInputMethodSubtype(inputMethodSubProperty).then((data) => {
        console.info('listInputMethodSubtype success');
    }).catch((err) => {
        console.error('listInputMethodSubtype promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
767 768 769 770 771
```

### listCurrentInputMethodSubtype<sup>9+</sup>

listCurrentInputMethodSubtype(callback: AsyncCallback&lt;Array&lt;InputMethodSubtype&gt;&gt;): void
Z
zhaolinglan 已提交
772

773 774 775 776 777 778 779 780 781 782 783 784 785
查询当前输入法的子类型列表。使用callback形式返回结果。参数个数为1,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名   | 类型                                               | 必填 | 说明                   |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
| callback | Array<[InputMethodSubtype](#InputMethodSubtype)> | 是   | 返回当前输入法的子类型列表。 |

**示例:**

```js
H
Hollokin 已提交
786 787 788 789 790 791 792 793 794 795 796
try {
    InputMethodSetting.listCurrentInputMethodSubtype((err,data) => {
        if (err) {
            console.error('listCurrentInputMethodSubtype failed because: ' + JSON.stringify(err));
            return;
        }
        console.log('listCurrentInputMethodSubtype success');
    });
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
```

### listCurrentInputMethodSubtype<sup>9+</sup>

listCurrentInputMethodSubtype(): Promise&lt;Array&lt;InputMethodSubtype&gt;&gt;

查询当前输入法的子类型列表。使用promise形式返回结果。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型                                                        | 说明                   |
| ----------------------------------------------------------- | ---------------------- |
| Promise<Array<[InputMethodSubtype](#InputMethodSubtype)>> | 返回当前输入法的子类型列表。 |

**示例:**

```js
H
Hollokin 已提交
816 817 818 819 820 821 822 823 824 825
try {
    InputMethodSetting.listCurrentInputMethodSubtype().then((data) => {
        console.info('listCurrentInputMethodSubtype success');
    }).catch((err) => {
        console.error('listCurrentInputMethodSubtype promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}

826 827 828 829 830
```

### getInputMethods<sup>9+</sup>

getInputMethods(enable: boolean, callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void
Z
zhaolinglan 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

获取已激活/未激活输入法列表。参数enable取true,返回已激活输入法列表,取false返回未激活输入法列表。使用callback形式返回结果。参数个数为2,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名   | 类型                                                | 必填 | 说明                          |
| -------- | --------------------------------------------------- | ---- | ----------------------------- |
| enable   | boolean                                             | 是   | 指定返回已激活/未激活。       |
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | 是   | 返回已激活/未激活输入法列表。 |

**示例:**

```js
H
Hollokin 已提交
846 847 848 849 850 851 852 853 854 855 856
try {
    InputMethodSetting.getInputMethods(true, (err,data) => {
        if (err) {
            console.error('getInputMethods failed because: ' + JSON.stringify(err));
            return;
        }
        console.log('getInputMethods success');
    });
} catch (err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
Z
zhaolinglan 已提交
857 858
```

859
### getInputMethods<sup>9+</sup>
Z
zhaolinglan 已提交
860

861
getInputMethods(enable: boolean): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
Z
zhaolinglan 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881

获取已激活/未激活输入法列表。参数enable取true返回已激活输入法列表,取false返回未激活输入法列表。使用promise形式返回结果。参数个数为0,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型    | 必填 | 说明                    |
| ------ | ------- | ---- | ----------------------- |
| enable | boolean | 是   | 指定返回已激活/未激活。 |

**返回值:**

| 类型                                                         | 说明                          |
| ------------------------------------------------------------ | ----------------------------- |
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | 返回已激活/未激活输入法列表。 |

**示例:**

```js
H
Hollokin 已提交
882 883 884 885 886 887 888 889 890
try {
    InputMethodSetting.getInputMethods(true).then((data) => {
        console.info('getInputMethods success');
    }).catch((err) => {
        console.error('getInputMethods promise err: ' + err);
    })
} catch(err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
Z
zhaolinglan 已提交
891 892
```

893
### listInputMethod<sup>deprecated</sup>
Z
zhouyongfei 已提交
894 895 896

listInputMethod(callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void

897
查询已安装的输入法列表。使用callback形式返回结果。参数个数为1,否则抛出异常。
Z
zhouyongfei 已提交
898

899
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
900

901
**参数:**
B
bmeangel 已提交
902

T
explain  
tianyu 已提交
903 904
| 参数名   | 类型                                               | 必填 | 说明                   |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
905
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | 是   | 返回已安装输入法列表。 |
Z
zhouyongfei 已提交
906

907
**示例:**
T
explain  
tianyu 已提交
908

909
```js
910
InputMethodSetting.listInputMethod((err,data) => {
Z
zhaolinglan 已提交
911
    if (err) {
Z
zhaolinglan 已提交
912
        console.error('listInputMethod failed because: ' + JSON.stringify(err));
913
        return;
914
    }
Z
zhaolinglan 已提交
915
    console.log('listInputMethod success');
916
 });
917
```
Z
zhouyongfei 已提交
918

919
### listInputMethod<sup>deprecated</sup>
Z
zhouyongfei 已提交
920

Z
zhaolinglan 已提交
921
listInputMethod(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
Z
zhouyongfei 已提交
922

923
查询已安装的输入法列表。使用promise形式返回结果。参数个数为0,否则抛出异常。
Z
zhouyongfei 已提交
924

925
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
926

927
**返回值:**
B
bmeangel 已提交
928

T
explain  
tianyu 已提交
929 930
| 类型                                                        | 说明                   |
| ----------------------------------------------------------- | ---------------------- |
931
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | 返回已安装输入法列表。 |
Z
zhouyongfei 已提交
932

933
**示例:**
934 935

```js
Z
zhaolinglan 已提交
936
InputMethodSetting.listInputMethod().then((data) => {
Z
zhaolinglan 已提交
937
    console.info('listInputMethod success');
Z
zhaolinglan 已提交
938
}).catch((err) => {
Z
zhaolinglan 已提交
939
    console.error('listInputMethod promise err: ' + err);
Z
zhaolinglan 已提交
940
})
941
```
Z
zhouyongfei 已提交
942

943
### displayOptionalInputMethod<sup>deprecated</sup>
Z
zhouyongfei 已提交
944 945 946

displayOptionalInputMethod(callback: AsyncCallback&lt;void&gt;): void

947
显示输入法选择对话框。使用callback形式返回结果。参数个数为1,否则抛出异常。
Z
zhouyongfei 已提交
948

949
**系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
950

951
**参数:**
Z
zhouyongfei 已提交
952 953 954 955 956

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

957
**示例:**
958 959

```js
960
InputMethodSetting.displayOptionalInputMethod((err) => {
Z
zhaolinglan 已提交
961
    if (err) {
Z
zhaolinglan 已提交
962
        console.error('displayOptionalInputMethod failed because: ' + JSON.stringify(err));
963 964
        return;
    }
Z
zhaolinglan 已提交
965
    console.info('displayOptionalInputMethod success');
966
});
967
```
Z
zhouyongfei 已提交
968

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
### showOptionalInputMethods<sup>9+</sup>

showOptionalInputMethods(callback: AsyncCallback&lt;void&gt;): void

显示输入法选择对话框。使用callback形式返回结果。参数个数为1,否则抛出异常。

**需要权限**: ohos.permission.CONNECT_IME_ABILITY

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

```js
H
Hollokin 已提交
988 989 990 991 992 993 994 995 996 997 998
try {
    InputMethodSetting.showOptionalInputMethods((err) => {
        if (err) {
            console.error('showOptionalInputMethods failed because: ' + JSON.stringify(err));
            return;
        }
        console.info('showOptionalInputMethods success');
    });
} catch (err) {
    console.error('err: ' + err.code + 'err message: ' + err.message);
}
999 1000
```

H
Hollokin 已提交
1001
### displayOptionalInputMethod<sup>deprecated</sup>
Z
zhouyongfei 已提交
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
displayOptionalInputMethod(): Promise&lt;void&gt;

显示输入法选择对话框。使用promise形式返回结果。参数个数为0,否则抛出异常。

**系统能力**: SystemCapability.MiscServices.InputMethodFramework

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

**示例:**

```js
InputMethodSetting.displayOptionalInputMethod().then(() => {
    console.info('displayOptionalInputMethod success.(promise)');
}).catch((err) => {
    console.error('displayOptionalInputMethod promise err: ' + err);
})
```

### showOptionalInputMethods<sup>9+</sup>

  showOptionalInputMethods(): Promise&lt;void&gt;
Z
zhouyongfei 已提交
1028

1029
  显示输入法选择对话框。使用promise形式返回结果。参数个数为0,否则抛出异常。
Z
zhouyongfei 已提交
1030

1031
  **系统能力**: SystemCapability.MiscServices.InputMethodFramework
Z
zhouyongfei 已提交
1032

1033
**返回值:**
Z
zhouyongfei 已提交
1034

Z
refresh  
zhouyongfei 已提交
1035
| 类型 | 说明 |
Z
zhouyongfei 已提交
1036
| -------- | -------- |
1037
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
Z
zhouyongfei 已提交
1038

1039
**示例:**
1040 1041

```js
1042
InputMethodSetting.showOptionalInputMethods().then(() => {
Z
zhaolinglan 已提交
1043
    console.info('displayOptionalInputMethod success.(promise)');
Z
zhaolinglan 已提交
1044
}).catch((err) => {
Z
zhaolinglan 已提交
1045
    console.error('displayOptionalInputMethod promise err: ' + err);
Z
zhaolinglan 已提交
1046
})
1047
```