# 切æ¢è¾“入法应用 输入法框架æœåŠ¡æä¾›äº†åˆ‡æ¢è¾“入法应用的API,支æŒåˆ‡æ¢è¾“入法ã€åˆ‡æ¢è¾“入法和å类型ã€åˆ‡æ¢å½“å‰è¾“入法的å类型。 > **说明:** > > 1. 以下接å£çš„使用仅å…许在当å‰è¾“入法应用ä¸è°ƒç”¨ã€‚ > > 2. 本示例å‡è®¾å·²ç»åœ¨è¾“å…¥æ³•åº”ç”¨ä¸æ‰§è¡Œï¼Œå¦‚果实现一个输入法应用,请å‚考[实现一个输入法应用](./inputmethod-application-guide.md)。 ## 切æ¢å½“å‰è¾“入法å类型 1. 在已完æˆä¸€ä¸ªè¾“入法应用的基础上,当输入法应用是当å‰è¾“入法时,在输入法应用ä¸ä½¿ç”¨[switchCurrentInputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchcurrentinputmethodsubtype9)接å£ï¼Œä¼ 入当å‰è¾“入法的å类型[InputMethodSubtype](../reference/apis-ime-kit/js-apis-inputmethod-subtype.md#inputmethodsubtype)ä½œä¸ºå‚æ•°å³å¯åˆ‡æ¢å½“å‰è¾“入法å类型。 ```ts import { InputMethodSubtype, inputMethod } from '@kit.IMEKit'; export class KeyboardController { async switchCurrentInputMethodSubtype() { let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype(); // 获å–当å‰è¾“入法的所有å类型 let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // 获å–当å‰è¾“入法当å‰çš„å类型 for(let i=0;i<subTypes.length;i++) { if(subTypes[i].id != currentSubType.id) { // 判æ–䏿˜¯å½“å‰çš„å类型时切æ¢ï¼Œå®žé™…å¼€å‘ä¸å¯ä»¥æ ¹æ®éœ€è¦å¡«å›ºå®šå类型 await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]); } } } } ``` 2. è¾“å…¥æ³•åº”ç”¨ä¸æ³¨å†Œå类型å˜åŒ–äº‹ä»¶ï¼Œæ ¹æ®ä¸åŒåç±»åž‹åŠ è½½ä¸åŒçš„输入界é¢ã€‚ ```ts import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit'; export class KeyboardController { async switchCurrentInputMethodSubtype() { let panel: inputMethodEngine.Panel; let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); // 设置监å¬å类型事件,改å˜è¾“å…¥æ³•åº”ç”¨ç•Œé¢ inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { if(inputMethodSubtype.id == 'InputMethodExtAbility') { panel.setUiContent('pages/Index'); // å‡è®¾åœ¨è¾“å…¥æ³•åº”ç”¨ä¸æ¤æ—¶Panelå·²ç»åœ¨onCreateæµç¨‹ä¸åˆ›å»º } if(inputMethodSubtype.id == 'InputMethodExtAbility1') { panel.setUiContent('pages/Index1'); // å‡è®¾åœ¨è¾“å…¥æ³•åº”ç”¨ä¸æ¤æ—¶Panelå·²ç»åœ¨onCreateæµç¨‹ä¸åˆ›å»º } }); } } ``` ## 切æ¢è¾“入法应用 在已完æˆä¸€ä¸ªè¾“入法应用的基础上,当输入法应用是当å‰è¾“入法时,在输入法应用ä¸ä½¿ç”¨[switchInputMethod](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodswitchinputmethod9)接å£ï¼Œä¼ å…¥ç›®æ ‡è¾“å…¥æ³•çš„[InputMethodProperty](../reference/apis-ime-kit/js-apis-inputmethod.md#inputmethodproperty8)ä¿¡æ¯ï¼Œå³å¯åˆ‡æ¢è¾“å…¥æ³•åˆ°ç›®æ ‡è¾“å…¥æ³•åº”ç”¨ã€‚ ```ts import { inputMethod } from '@kit.IMEKit'; export class KeyboardController { async switchInputMethod(){ let inputMethods = await inputMethod.getSetting().getInputMethods(true); // 获å–已使能的输入法列表 let currentInputMethod = inputMethod.getCurrentInputMethod(); // 获å–当å‰è¾“入法 for(let i=0;i<inputMethods.length;i++) { if(inputMethods[i].name != currentInputMethod.name) { // 判æ–䏿˜¯å½“å‰è¾“入法时,切æ¢åˆ°è¯¥è¾“入法,实际开å‘ä¸å¯ä»¥åˆ‡æ¢åˆ°å›ºå®šè¾“入法 await inputMethod.switchInputMethod(inputMethods[i]); } } } } ``` ## 切æ¢è¾“入法应用和å类型 在已完æˆä¸€ä¸ªè¾“入法应用的基础上,当输入法应用是当å‰è¾“入法时,在输入法应用ä¸ä½¿ç”¨[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)ä¿¡æ¯ï¼Œå³å¯åˆ‡æ¢è¾“å…¥æ³•åˆ°ç›®æ ‡è¾“å…¥æ³•åº”ç”¨çš„ç›®æ ‡å类型。 ```ts import { inputMethod } from '@kit.IMEKit'; export class KeyboardController { async switchInputMethodAndSubtype() { let inputMethods = await inputMethod.getSetting().getInputMethods(true); // 获å–已使能的输入法列表 let currentInputMethod = inputMethod.getCurrentInputMethod(); // 获å–当å‰è¾“入法 for (let i = 0;i < inputMethods.length; i++) { if (inputMethods[i].name != currentInputMethod.name) { // 判æ–䏿˜¯å½“å‰è¾“入法时,切æ¢åˆ°è¯¥è¾“入法,实际开å‘ä¸å¯ä»¥åˆ‡æ¢åˆ°å›ºå®šè¾“入法 let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // 获å–ç›®æ ‡è¾“å…¥æ³•çš„å类型 await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // 本示例默认切æ¢åˆ°èŽ·å–的第一个å类型 } } } } ```