js-apis-inputmethod.md 6.3 KB
Newer Older
Z
zengyawen 已提交
1 2
# Input Method Framework

E
ester.zhou 已提交
3
> **NOTE**
E
ester.zhou 已提交
4
>
E
ester.zhou 已提交
5
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6 7 8 9 10 11 12 13


## Modules to Import

```
import inputMethod from '@ohos.inputMethod';
```

E
ester.zhou 已提交
14
## inputMethod<sup>6+</sup>
Z
zengyawen 已提交
15 16 17

Provides the constants.

E
ester.zhou 已提交
18
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
19 20 21 22 23 24

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| MAX_TYPE_NUM | number | Yes| No| Maximum number of supported input methods.|


E
ester.zhou 已提交
25
## InputMethodProperty<sup>6+</sup>
Z
zengyawen 已提交
26 27 28

Describes the input method application attributes.

E
ester.zhou 已提交
29
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
30 31 32 33 34 35

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| packageName | string | Yes| No| Package name.|
| methodId | string | Yes| No| Ability name.|

E
ester.zhou 已提交
36
## inputMethod.getInputMethodController
Z
zengyawen 已提交
37 38 39 40 41

getInputMethodController(): InputMethodController

Obtains an [InputMethodController](#InputMethodController) instance.

E
ester.zhou 已提交
42
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
43

E
ester.zhou 已提交
44
**Return value**
Z
zengyawen 已提交
45

E
ester.zhou 已提交
46 47 48 49 50 51
| Type| Description|
| -------- | -------- |
| [InputMethodController](#InputMethodController) | Returns the current **InputMethodController** instance.|

**Example**

E
ester.zhou 已提交
52
```js
E
ester.zhou 已提交
53
  var InputMethodController = inputMethod.getInputMethodController();
E
ester.zhou 已提交
54
```
Z
zengyawen 已提交
55

E
ester.zhou 已提交
56
## inputMethod.getInputMethodSetting<sup>6+</sup>
Z
zengyawen 已提交
57 58 59 60 61

getInputMethodSetting(): InputMethodSetting

Obtains an [InputMethodSetting](#InputMethodSetting) instance.

E
ester.zhou 已提交
62
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
63

E
ester.zhou 已提交
64
**Return value**
Z
zengyawen 已提交
65

E
ester.zhou 已提交
66 67 68
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
| [InputMethodSetting](#InputMethodSetting) | Returns the current **InputMethodSetting** instance.|
Z
zengyawen 已提交
69

E
ester.zhou 已提交
70
**Example**
Z
zengyawen 已提交
71

E
ester.zhou 已提交
72
```js
E
ester.zhou 已提交
73
  var InputMethodSetting = inputMethod.getInputMethodSetting();
E
ester.zhou 已提交
74
```
Z
zengyawen 已提交
75

E
ester.zhou 已提交
76
## InputMethodController
Z
zengyawen 已提交
77 78 79 80 81 82 83 84 85

In the following API examples, you must first use [getInputMethodController](#getInputMethodController) to obtain an **InputMethodController** instance, and then call the APIs using the obtained instance.

### stopInput

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

Hides the keyboard. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
86
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
87

E
ester.zhou 已提交
88
**Parameters**
Z
zengyawen 已提交
89

E
ester.zhou 已提交
90 91 92
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the keyboard is successfully hidden.|
Z
zengyawen 已提交
93

E
ester.zhou 已提交
94
**Example**
Z
zengyawen 已提交
95

E
ester.zhou 已提交
96
```js
Z
zengyawen 已提交
97 98 99 100 101 102 103 104 105 106 107
 InputMethodController.stopInput((error)=>{
     console.info('stopInput');
 });
```

### stopInput

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

Hides the keyboard. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
108
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
109

E
ester.zhou 已提交
110
**Return value**
Z
zengyawen 已提交
111

E
ester.zhou 已提交
112 113 114
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return whether the keyboard is successfully hidden.|
Z
zengyawen 已提交
115

E
ester.zhou 已提交
116
**Example**
Z
zengyawen 已提交
117 118


E
ester.zhou 已提交
119
```js
E
ester.zhou 已提交
120 121
  var isSuccess = InputMethodController.stopInput();
  console.info('stopInput isSuccess = ' + isSuccess);
Z
zengyawen 已提交
122 123
```

E
ester.zhou 已提交
124
## InputMethodSetting<sup>6+</sup>
Z
zengyawen 已提交
125 126 127 128 129 130 131 132 133

In the following API examples, you must first use [getInputMethodSetting](#getInputMethodSetting) to obtain an **InputMethodSetting** instance, and then call the APIs using the obtained instance.

### listInputMethod

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

Obtains the list of installed input methods. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
134
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
135

E
ester.zhou 已提交
136 137 138 139 140 141 142 143
**Parameters**

| Name  | Type                                              | Mandatory| Description                  |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
| callback | Array<[InputMethodProperty](#InputMethodProperty)> | Yes  | Callback used to return the list of installed input methods.|

**Example**

E
ester.zhou 已提交
144 145 146 147 148 149
```js
  InputMethodSetting.listInputMethod((properties)=>{
    for (var i = 0;i < properties.length; i++) {
     var property = properties[i];
     console.info(property.packageName + "/" + property.methodId);
    }
E
ester.zhou 已提交
150
  });
E
ester.zhou 已提交
151
```
Z
zengyawen 已提交
152 153 154 155 156 157 158

### listInputMethod

listInputMethod(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;

Obtains the list of installed input methods. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
159
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
160

E
ester.zhou 已提交
161
**Return value**
Z
zengyawen 已提交
162

E
ester.zhou 已提交
163 164 165 166 167 168 169
| Type                                                       | Description                  |
| ----------------------------------------------------------- | ---------------------- |
| Promise<Array<[InputMethodProperty](#InputMethodProperty)>> | Promise used to return the list of installed input methods.|

**Example**

```js
E
ester.zhou 已提交
170 171 172 173 174
  var properties = InputMethodSetting.listInputMethod();
  for (var i = 0;i < properties.length; i++) {
    var property = properties[i];
    console.info(property.packageName + "/" + property.methodId);
  }
E
ester.zhou 已提交
175
```
Z
zengyawen 已提交
176 177 178 179 180 181 182

### displayOptionalInputMethod

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

Displays a dialog box for selecting an input method. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
183
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
184 185 186 187 188 189 190

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

E
ester.zhou 已提交
191 192
**Example**
```js
E
ester.zhou 已提交
193
  InputMethodSetting.displayOptionalInputMethod(()=>{
E
ester.zhou 已提交
194
   console.info('displayOptionalInputMethod is called');
E
ester.zhou 已提交
195
   });
E
ester.zhou 已提交
196
```
Z
zengyawen 已提交
197 198 199 200 201 202 203

### displayOptionalInputMethod

displayOptionalInputMethod(): Promise&lt;void&gt;

Displays a dialog box for selecting an input method. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
204
**System capability**: SystemCapability.MiscServices.InputMethodFramework
Z
zengyawen 已提交
205

E
ester.zhou 已提交
206
**Return value**
Z
zengyawen 已提交
207 208 209 210 211

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the execution result.|

E
ester.zhou 已提交
212 213 214
**Example**

```js
E
ester.zhou 已提交
215
  InputMethodSetting.displayOptionalInputMethod();
E
ester.zhou 已提交
216
```