js-apis-inputmethod.md 8.5 KB
Newer Older
E
ester.zhou 已提交
1 2
# Input Method Framework

3 4 5
The **inputMethod** module provides an input method framework, which can be used to hide the keyboard, obtain the list of installed input methods, display the dialog box for input method selection, and more.

>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
> 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.
E
ester.zhou 已提交
8 9 10 11 12


## Modules to Import

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

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

Provides the constants.

20
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
21 22 23 24 25 26

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


E
ester.zhou 已提交
27
## InputMethodProperty<sup>8+</sup>
E
ester.zhou 已提交
28 29 30

Describes the input method application attributes.

31
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
32 33 34 35 36 37

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

E
ester.zhou 已提交
38
## inputMethod.getInputMethodController
E
ester.zhou 已提交
39 40 41

getInputMethodController(): InputMethodController

42
Obtains an **[InputMethodController](#inputmethodcontroller)** instance.
E
ester.zhou 已提交
43

44
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
45

E
ester.zhou 已提交
46
**Return value**
E
ester.zhou 已提交
47

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

**Example**

E
ester.zhou 已提交
54
```js
E
ester.zhou 已提交
55
  var InputMethodController = inputMethod.getInputMethodController();
E
ester.zhou 已提交
56
```
E
ester.zhou 已提交
57

E
ester.zhou 已提交
58
## inputMethod.getInputMethodSetting<sup>8+</sup>
E
ester.zhou 已提交
59 60 61

getInputMethodSetting(): InputMethodSetting

62
Obtains an **[InputMethodSetting](#inputmethodsetting8)** instance.
E
ester.zhou 已提交
63

64
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
65

E
ester.zhou 已提交
66
**Return value**
E
ester.zhou 已提交
67

E
ester.zhou 已提交
68 69
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
70 71
| [InputMethodSetting](#inputmethodsetting8) | Returns the current **InputMethodSetting** instance.|

E
ester.zhou 已提交
72

E
ester.zhou 已提交
73
**Example**
E
ester.zhou 已提交
74

E
ester.zhou 已提交
75
```js
E
ester.zhou 已提交
76
  var InputMethodSetting = inputMethod.getInputMethodSetting();
E
ester.zhou 已提交
77
```
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
## inputMethod.switchInputMethod<sup>9+</sup>

switchInputMethod(target: InputmethodProperty, callback: AsyncCallback&lt;boolean&gt;): void;

Switches to another input method. This API uses an asynchronous callback to return the result. This API can be used only in the stage model.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
|target | [InputmethodProperty](#inputmethodproperty8) | Yes| Input method to switch to.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result. |


**Example**

```js
  inputMethod.switchInputMethod({packageName:"com.ohos.inputApp", methodId:"InputDemoService"}).then(res => {
     prompt.showToast({message:"Input method switched." + this.imeList[this.flag].packageName, duration: 200});
 });
```
## inputMethod.switchInputMethod<sup>9+</sup>
switchInputMethod(target: InputmethodProperty): Promise&lt;boolean&gt;

Switches to another input method. This API uses a promise to return the result. This API can be used only in the stage model.
E
ester.zhou 已提交
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
|target |  [InputmethodProperty](#inputmethodproperty8)| Yes| Input method to switch to.|

**Return value**
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
| Promise\<boolean> | Promise used to return the execution result. |

**Example**


```js
  inputMethod.switchInputMethod({packageName:"com.ohos.inputApp", methodId:"InputDemoService"}).then(res => {
     prompt.showToast({message:"Input method switched." + this.imeList[this.flag].packageName, duration: 200});
 });
```
E
ester.zhou 已提交
127
## InputMethodController
E
ester.zhou 已提交
128

S
shawn_he 已提交
129
In the following API examples, you must first use [getInputMethodController](#inputmethodgetinputmethodcontroller) to obtain an **InputMethodController** instance, and then call the APIs using the obtained instance.
E
ester.zhou 已提交
130 131 132 133 134 135 136

### stopInput

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

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

137
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
138

E
ester.zhou 已提交
139
**Parameters**
E
ester.zhou 已提交
140

E
ester.zhou 已提交
141 142 143
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the keyboard is successfully hidden.|
E
ester.zhou 已提交
144

E
ester.zhou 已提交
145
**Example**
E
ester.zhou 已提交
146

E
ester.zhou 已提交
147
```js
E
ester.zhou 已提交
148
  InputMethodController.stopInput((error)=>{
E
ester.zhou 已提交
149 150 151 152 153 154 155 156 157 158
     console.info('stopInput');
 });
```

### stopInput

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

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

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

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

E
ester.zhou 已提交
163 164
| Type| Description|
| -------- | -------- |
165
| Promise&lt;boolean&gt; | Promise used to return whether the keyboard is successfully hidden. |
E
ester.zhou 已提交
166

E
ester.zhou 已提交
167
**Example**
E
ester.zhou 已提交
168 169


E
ester.zhou 已提交
170
```js
E
ester.zhou 已提交
171 172
  var isSuccess = InputMethodController.stopInput();
  console.info('stopInput isSuccess = ' + isSuccess);
E
ester.zhou 已提交
173 174
```

E
ester.zhou 已提交
175
## InputMethodSetting<sup>8+</sup>
E
ester.zhou 已提交
176

S
shawn_he 已提交
177
In the following API examples, you must first use [getInputMethodSetting](#inputmethodgetinputmethodcontroller) to obtain an **InputMethodSetting** instance, and then call the APIs using the obtained instance.
E
ester.zhou 已提交
178 179 180 181 182 183 184

### 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.

185
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
186

E
ester.zhou 已提交
187 188 189
**Parameters**
| Name  | Type                                              | Mandatory| Description                  |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
190
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | Yes  | Callback used to return the list of installed input methods.|
E
ester.zhou 已提交
191 192 193

**Example**

E
ester.zhou 已提交
194 195 196 197 198 199
```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 已提交
200
  });
E
ester.zhou 已提交
201
```
E
ester.zhou 已提交
202 203 204

### listInputMethod

E
ester.zhou 已提交
205
listInputMethod(): Array&lt;InputMethodProperty&gt;
E
ester.zhou 已提交
206 207 208

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

209
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
210

E
ester.zhou 已提交
211 212 213
**Return value**
| Type                                                       | Description                  |
| ----------------------------------------------------------- | ---------------------- |
214
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return the list of installed input methods. |
E
ester.zhou 已提交
215 216 217 218

**Example**

```js
E
ester.zhou 已提交
219 220 221 222 223
  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 已提交
224
```
E
ester.zhou 已提交
225 226 227 228 229 230 231

### 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.

232
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
233

234
**Parameters**
E
ester.zhou 已提交
235 236 237 238 239

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

E
ester.zhou 已提交
240
**Example**
241

E
ester.zhou 已提交
242
```js
E
ester.zhou 已提交
243 244 245
  InputMethodSetting.displayOptionalInputMethod(()=>{
    console.info('displayOptionalInputMethod is called');
  });
E
ester.zhou 已提交
246
```
E
ester.zhou 已提交
247 248 249

### displayOptionalInputMethod

E
ester.zhou 已提交
250
  displayOptionalInputMethod(): Promise&lt;void&gt;
E
ester.zhou 已提交
251 252 253

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

254
  **System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
255

E
ester.zhou 已提交
256
**Return value**
E
ester.zhou 已提交
257 258 259

| Type| Description|
| -------- | -------- |
260
| Promise&lt;void&gt; | Promise used to return the execution result. |
E
ester.zhou 已提交
261

E
ester.zhou 已提交
262 263 264
**Example**

```js
E
ester.zhou 已提交
265
  InputMethodSetting.displayOptionalInputMethod();
266
```