harmony 鸿蒙Switching Between Input Methods

  • 2025-06-12
  • 浏览 (6)

Switching Between Input Methods

You can use the APIs of the input method framework service to easily switch between input methods and input method subtypes.

NOTE

  1. The following APIs can be called only in the current input method application.

  2. For details about how to implement an input method application, see Implementing an Input Method Application.

Switching Between Input Method Subtypes

  1. In the input method application in use, call switchCurrentInputMethodSubtype with the target InputMethodSubtype to switch to another subtype of the current input method.
   import { InputMethodSubtype, inputMethod } from '@kit.IMEKit';
   
   export class KeyboardController {
     async switchCurrentInputMethodSubtype() {
       let subTypes = await inputMethod.getSetting().listCurrentInputMethodSubtype(); // Obtain all subtypes of the current input method.
       let currentSubType = inputMethod.getCurrentInputMethodSubtype(); // Obtain the current subtype of the current input method.
       for(let i=0;i<subTypes.length;i++) {
         if(subTypes[i].id != currentSubType.id) { // If the current subtype is not the specified one, switch to the specified one. You can enter a fixed subtype as required.
           await inputMethod.switchCurrentInputMethodSubtype(subTypes[i]);
         }
       }
     }
   }
  1. Register a listener in the input method application for subtype changes, so as to load a subtype-specific soft keyboard UI.
   import { InputMethodSubtype, inputMethodEngine, inputMethod } from '@kit.IMEKit';

   export class KeyboardController {
     async switchCurrentInputMethodSubtype() {
       let panel: inputMethodEngine.Panel;
       let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
       // Register a listener in the input method application for subtype changes.
       inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
         if(inputMethodSubtype.id == 'InputMethodExtAbility') {
           panel.setUiContent('pages/Index'); // Assume that the panel has been created in the onCreate process in the input method application.
         }
         if(inputMethodSubtype.id == 'InputMethodExtAbility1') {
           panel.setUiContent('pages/Index1'); // Assume that the panel has been created in the onCreate process in the input method application.
         }
       });
     }
   }

Switching Between Input Methods

In the input method application in use, call switchInputMethod with the target InputMethodProperty to switch to another input method.

import { inputMethod } from '@kit.IMEKit';

export class KeyboardController {
  async switchInputMethod(){
    let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods.
    let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method.
    for(let i=0;i<inputMethods.length;i++) {
      if(inputMethods[i].name != currentInputMethod.name) { // If the current input method is not the specified one, switch to the specified one. You can enter a fixed input method as required.
        await inputMethod.switchInputMethod(inputMethods[i]);
      }
    }
  }
}

Switching Between Input Methods and Subtypes

In the input method application in use, call switchCurrentInputMethodAndSubtype with the target InputMethodProperty and InputMethodSubtype to switch to the subtype of another input method.

import { inputMethod } from '@kit.IMEKit';

export class KeyboardController {
  async switchInputMethodAndSubtype() {
    let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods.
    let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method.
    for (let i = 0;i < inputMethods.length; i++) {
      if (inputMethods[i].name != currentInputMethod.name) { // If the current input method is not the specified one, switch to the specified one. You can enter a fixed input method as required.
        let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputMethods[i]); // Obtain the subtypes of the target input method.
        await inputMethod.switchCurrentInputMethodAndSubtype(inputMethods[i], subTypes[0]); // This example switches to the first obtained subtype.
      }
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙IME Kit

harmony 鸿蒙Introduction to IME Kit

harmony 鸿蒙Setting Input Method Subtypes

harmony 鸿蒙Implementing an Input Method Application

harmony 鸿蒙Immersive Mode of the Input Method Application

harmony 鸿蒙Using the Input Method in a Custom Edit Box (C/C++)

harmony 鸿蒙Using the Input Method in a Custom Edit Box

0  赞