1# 输入法子类型开发指南 2<!--Kit: IME Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @illybyy--> 5<!--Designer: @andeszhang--> 6<!--Tester: @murphy1984--> 7<!--Adviser: @zhang_yixin13--> 8 9输入法子类型允许输入法展现不同的输入模式或语言,用户可以根据需要在不同模式和语言中切换。如:输入法的中文键盘、英文键盘等都属于输入法的子类型。 10 11## 输入法子类型的配置与实现 12 131. 输入法应用开发者只需要注册实现一个InputMethodExtensionAbility,所有的输入法子类型共用该InputMethodExtensionAbility,在[module.json5配置文件](../quick-start/module-configuration-file.md)中添加metadata,name为ohos_extension.input_method,用于配置所有子类型的资源信息。 14 ```ts 15 { 16 "module": { 17 // ... 18 "extensionAbilities": [ 19 { 20 "description": "InputMethodExtDemo", 21 "icon": "$media:icon", 22 "name": "InputMethodExtAbility", 23 "srcEntry": "./ets/InputMethodExtensionAbility/InputMethodService.ts", 24 "type": "inputMethod", 25 "exported": true, 26 "metadata": [ 27 { 28 "name": "ohos.extension.input_method", 29 "resource": "$profile:input_method_config" 30 } 31 ] 32 } 33 ] 34 } 35 } 36 ``` 37 382. 子类型配置文件格式如下,字段释义参照[InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype),开发者需要严格按照配置文件格式及字段进行子类型信息配置,locale字段的配置参照[i18n-locale-culture](.././internationalization/i18n-locale-culture.md#实现原理)。 39 ``` 40 { 41 "subtypes": [ 42 { 43 "icon": "$media:icon", 44 "id": "InputMethodExtAbility", 45 "label": "$string:english", 46 "locale": "en-US", 47 "mode": "lower" 48 }, 49 { 50 "icon": "$media:icon", 51 "id": "InputMethodExtAbility1", 52 "label": "$string:chinese", 53 "locale": "zh-CN", 54 "mode": "lower" 55 } 56 ] 57 } 58 ``` 59 603. 输入法应用中监听子类型事件,根据不同的子类型,可以加载不同的软键盘界面,或者使用状态变量控制界面中的软键盘显示。 61 62 ```ts 63 import { InputMethodSubtype, inputMethodEngine } from '@kit.IMEKit'; 64 65 let panelInfo: inputMethodEngine.PanelInfo = { 66 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 67 flag: inputMethodEngine.PanelFlag.FLG_FIXED 68 }; 69 let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 70 // createPanel需要在InputMethodExtensionAbility的Create声明周期中完成,this.context是InputMethodExtensionAbility中的InputMethodExtensionContext 71 inputMethodAbility.createPanel(this.context, panelInfo).then(async (panel: inputMethodEngine.Panel) => { 72 let inputPanel: inputMethodEngine.Panel = panel; 73 inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { 74 // 保存当前输入法子类型, 此处也可以改变状态变量的值,布局中判断状态变量,不同的子类型显示不同的布局控件 75 let subType: InputMethodSubtype = inputMethodSubtype; 76 if (inputMethodSubtype.id == 'InputMethodExtAbility') { // 根据不同的子类型,可以加载不同的软键盘界面 77 inputPanel.setUiContent('pages/Index'); 78 } 79 if (inputMethodSubtype.id == 'InputMethodExtAbility1') { // 根据不同的子类型,可以加载不同的软键盘界面 80 inputPanel.setUiContent('pages/Index1'); 81 } 82 }); 83 }).catch((err: BusinessError) => { 84 console.log(`Failed to createPanel, code: ${err.code}, message: ${err.message}`); 85 }); 86 ``` 87 88## 输入法子类型相关信息的获取 89 901. 开发者可以通过调用[getCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodgetcurrentinputmethodsubtype9)获取当前输入法应用的当前子类型。 91 922. 开发者可以通过调用[listCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#listcurrentinputmethodsubtype9)获取当前输入法应用的所有子类型。 93 943. 开发者可以通过调用[listInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#listinputmethodsubtype9)获取指定输入法应用的所有子类型。 95 96 97## 输入法子类型的切换 98 991. 开发者可以通过调用[switchCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodsubtype9)切换当前输入法应用的子类型。 100 1012. 开发者可以通过调用[switchCurrentInputMethodAndSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodandsubtype9)切换至指定输入法应用的指定子类型。 102