1# 切换输入法应用 2<!--Kit: IME Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @illybyy--> 5<!--Designer: @andeszhang--> 6<!--Tester: @murphy1984--> 7<!--Adviser: @zhang_yixin13--> 8 9输入法框架服务提供了切换输入法应用的API,支持切换输入法、切换输入法和子类型、切换当前输入法的子类型。 10 11> **说明:** 12> 13> 1. 以下接口的使用仅允许在当前输入法应用中调用。 14> 15> 2. 本示例假设已经在输入法应用中执行,如何实现一个输入法应用,请参考[实现一个输入法应用](./inputmethod-application-guide.md)开发指导。 16 17## 切换当前输入法子类型 18 191. 在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[switchCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodsubtype9)接口,传入当前输入法的子类型[InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype)作为参数即可切换当前输入法子类型。 20 21 ```ts 22 import { InputMethodSubtype, inputMethod } from '@kit.IMEKit'; 23 24 export class KeyboardController { 25 async switchCurrentInputMethodSubtype() { 26 try { 27 let subTypes: Array<InputMethodSubtype> = 28 await inputMethod.getSetting().listCurrentInputMethodSubtype(); // 获取当前输入法的所有子类型 29 let currentSubType: InputMethodSubtype = inputMethod.getCurrentInputMethodSubtype(); // 获取当前输入法当前的子类型 30 if (subTypes.length === 0) { 31 return; 32 } 33 for (let i = 0; i < subTypes.length; i++) { 34 if (subTypes[i].id != currentSubType.id) { // 判断不是当前的子类型时切换,实际开发中可以根据需要填固定子类型 35 await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]); 36 return; 37 } 38 } 39 } catch (err) { 40 let error: BusinessError = err as BusinessError; 41 console.error(`Failed to switchCurrentInputMethodSubtype, code: ${err.code}, message: ${err.message}`); 42 } 43 } 44 } 45 ``` 46 472. 输入法应用中注册子类型变化事件,根据不同子类型加载不同的输入界面。 48 49 ```ts 50 import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit'; 51 52 export class KeyboardController { 53 async switchCurrentInputMethodSubtype() { 54 let panel: inputMethodEngine.Panel; // 此处panel需要在createPanel接口创建panel实例后使用 55 let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 56 // 设置监听子类型事件,改变输入法应用界面 57 inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { 58 if(inputMethodSubtype.id == 'InputMethodExtAbility') { 59 panel.setUiContent('pages/Index'); // 假设在输入法应用中此时Panel已经在onCreate流程中创建 60 } 61 if(inputMethodSubtype.id == 'InputMethodExtAbility1') { 62 panel.setUiContent('pages/Index1'); // 假设在输入法应用中此时Panel已经在onCreate流程中创建 63 } 64 }); 65 } 66 } 67 ``` 68 69## 切换输入法应用 70 71在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[switchInputMethod](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchinputmethod9)接口,传入目标输入法的[InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8)信息,即可切换输入法到目标输入法应用。 72 73```ts 74import { inputMethod } from '@kit.IMEKit'; 75 76export class KeyboardController { 77 async switchInputMethod(){ 78 try { 79 let inputMethods: Array<inputMethod.InputMethodProperty> = 80 await inputMethod.getSetting().getInputMethods(true); // 获取已使能的输入法列表 81 let currentInputMethod: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod(); // 获取当前输入法 82 for (let i = 0; i < inputMethods.length; i++) { 83 if (inputMethods[i].name != currentInputMethod.name) { // 判断不是当前输入法时,切换到该输入法,实际开发中可以切换到固定输入法 84 await inputMethod.switchInputMethod(inputMethods[i]); 85 return; 86 } 87 } 88 } catch (err) { 89 let error: BusinessError = err as BusinessError; 90 console.error(`Failed to switchInputMethod, code: ${err.code}, message: ${err.message}`); 91 } 92 } 93} 94``` 95 96## 切换输入法应用和子类型 97 98在已完成一个输入法应用的基础上,当输入法应用是当前输入法时,在输入法应用中使用[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)信息,即可切换输入法到目标输入法应用的目标子类型。 99 100```ts 101import { inputMethod } from '@kit.IMEKit'; 102 103export class KeyboardController { 104 async switchInputMethodAndSubtype() { 105 try { 106 let inputMethods: Array<inputMethod.InputMethodProperty> = 107 await inputMethod.getSetting().getInputMethods(true); // 获取已使能的输入法列表 108 let currentInputMethod: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod(); // 获取当前输入法 109 for (let i = 0; i < inputMethods.length; i++) { 110 if (inputMethods[i].name != currentInputMethod.name) { // 判断不是当前输入法时,切换到该输入法,实际开发中可以切换到固定输入法 111 let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // 获取目标输入法的子类型 112 if (subTypes.length > 0) { 113 await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // 本示例默认切换到获取的第一个子类型 114 } 115 return; 116 } 117 } 118 } catch (err) { 119 let error: BusinessError = err as BusinessError; 120 console.error(`Failed to switchCurrentInputMethodAndSubtype, code: ${err.code}, message: ${err.message}`); 121 } 122 } 123} 124```