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

E
ester.zhou 已提交
3
> **NOTE**<br>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 已提交
4
>
E
ester.zhou 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42


## Modules to Import

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

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

Provides the constants.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

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


## InputMethodProperty<sup>8+</sup><a name="InputMethodProperty"></a>

Describes the input method application attributes.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

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

## inputMethod.getInputMethodController<a name="getInputMethodController"></a>

getInputMethodController(): InputMethodController

Obtains an [InputMethodController](#InputMethodController) instance.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

E
ester.zhou 已提交
43
**Return value**
E
ester.zhou 已提交
44

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

**Example**

E
ester.zhou 已提交
51 52 53
  ```js
  var InputMethodController = inputMethod.getInputMethodController();
  ```
E
ester.zhou 已提交
54 55 56 57 58 59 60 61 62

## inputMethod.getInputMethodSetting<sup>8+</sup><a name="getInputMethodSetting"></a>

getInputMethodSetting(): InputMethodSetting

Obtains an [InputMethodSetting](#InputMethodSetting) instance.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

E
ester.zhou 已提交
63
**Return value**
E
ester.zhou 已提交
64

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

E
ester.zhou 已提交
69
**Example**
E
ester.zhou 已提交
70

E
ester.zhou 已提交
71 72 73
```js
var InputMethodSetting = inputMethod.getInputMethodSetting();
```
E
ester.zhou 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86

## InputMethodController<a name="InputMethodController"></a>

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.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

E
ester.zhou 已提交
87
**Parameters**
E
ester.zhou 已提交
88

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

E
ester.zhou 已提交
93
**Example**
E
ester.zhou 已提交
94

E
ester.zhou 已提交
95
```js
E
ester.zhou 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108
 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.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

E
ester.zhou 已提交
109
**Return value**
E
ester.zhou 已提交
110

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

E
ester.zhou 已提交
115
**Example**
E
ester.zhou 已提交
116 117


E
ester.zhou 已提交
118
```js
E
ester.zhou 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 var isSuccess = InputMethodController.stopInput();
 console.info('stopInput isSuccess = ' + isSuccess);
```

## InputMethodSetting<sup>8+</sup><a name="InputMethodSetting"></a>

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.

**System capability**: SystemCapability.Miscservices.InputMethodFramework

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

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

**Example**

E
ester.zhou 已提交
143 144 145 146 147 148 149 150
  ```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 已提交
151 152 153

### listInputMethod

E
ester.zhou 已提交
154
listInputMethod(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
E
ester.zhou 已提交
155 156 157 158 159

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

**System capability**: SystemCapability.Miscservices.InputMethodFramework

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

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

**Example**

```js
 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189

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

**System capability**: SystemCapability.Miscservices.InputMethodFramework

- Parameters

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

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

### 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 已提交
203
**System capability**: SystemCapability.Miscservices.InputMethodFramework
E
ester.zhou 已提交
204

E
ester.zhou 已提交
205
**Return value**
E
ester.zhou 已提交
206 207 208 209 210

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

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

```js
 InputMethodSetting.displayOptionalInputMethod();
```