1# 切换输入法应用 2 3输入法框架服务提供了切换输入法应用的API,支持切换输入法、切换输入法和子类型、切换当前输入法的子类型。 4 5> **说明:** 6> 7> 1. 以下接口的使用仅允许在当前输入法应用中调用。 8> 9> 2. 本示例假设已经在输入法应用中执行,如果实现一个输入法应用,请参考[实现一个输入法应用](./inputmethod-application-guide.md)。 10 11## 切换当前输入法子类型 12 131. 在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[switchCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodsubtype9)接口,传入当前输入法的子类型[InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype)作为参数即可切换当前输入法子类型。 14 15 ```ts 16 import { InputMethodSubtype, inputMethod } from '@kit.IMEKit'; 17 18 export class KeyboardController { 19 async switchCurrentInputMethodSubtype() { 20 let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype(); // 获取当前输入法的所有子类型 21 let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // 获取当前输入法当前的子类型 22 for(let i=0;i<subTypes.length;i++) { 23 if(subTypes[i].id != currentSubType.id) { // 判断不是当前的子类型时切换,实际开发中可以根据需要填固定子类型 24 await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]); 25 } 26 } 27 } 28 } 29 ``` 30 312. 输入法应用中注册子类型变化事件,根据不同子类型加载不同的输入界面。 32 33 ```ts 34 import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit'; 35 36 export class KeyboardController { 37 async switchCurrentInputMethodSubtype() { 38 let panel: inputMethodEngine.Panel; 39 let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 40 // 设置监听子类型事件,改变输入法应用界面 41 inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { 42 if(inputMethodSubtype.id == 'InputMethodExtAbility') { 43 panel.setUiContent('pages/Index'); // 假设在输入法应用中此时Panel已经在onCreate流程中创建 44 } 45 if(inputMethodSubtype.id == 'InputMethodExtAbility1') { 46 panel.setUiContent('pages/Index1'); // 假设在输入法应用中此时Panel已经在onCreate流程中创建 47 } 48 }); 49 } 50 } 51 52 53 ``` 54 55## 切换输入法应用 56 57在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[switchInputMethod](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchinputmethod9)接口,传入目标输入法的[InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8)信息,即可切换输入法到目标输入法应用。 58 59```ts 60import { inputMethod } from '@kit.IMEKit'; 61 62export class KeyboardController { 63 async switchInputMethod(){ 64 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // 获取已使能的输入法列表 65 let currentInputMethod = inputMethod.getCurrentInputMethod(); // 获取当前输入法 66 for(let i=0;i<inputMethods.length;i++) { 67 if(inputMethods[i].name != currentInputMethod.name) { // 判断不是当前输入法时,切换到该输入法,实际开发中可以切换到固定输入法 68 await inputMethod.switchInputMethod(inputMethods[i]); 69 } 70 } 71 } 72} 73``` 74 75## 切换输入法应用和子类型 76 77在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[switchCurrentInputMethodAndSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodandsubtype9)接口,传入目标输入法的[InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8),目标输入法的子类型[InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype)信息,即可切换输入法到目标输入法应用的目标子类型。 78 79```ts 80import { inputMethod } from '@kit.IMEKit'; 81 82export class KeyboardController { 83 async switchInputMethodAndSubtype() { 84 let inputMethods = await inputMethod.getSetting().getInputMethods(true); // 获取已使能的输入法列表 85 let currentInputMethod = inputMethod.getCurrentInputMethod(); // 获取当前输入法 86 for (let i = 0;i < inputMethods.length; i++) { 87 if (inputMethods[i].name != currentInputMethod.name) { // 判断不是当前输入法时,切换到该输入法,实际开发中可以切换到固定输入法 88 let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // 获取目标输入法的子类型 89 await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // 本示例默认切换到获取的第一个子类型 90 } 91 } 92 } 93} 94```