harmony 鸿蒙Setting Input Method Subtypes

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

Setting Input Method Subtypes

The input method subtypes allow the input method to switch to a specific mode or language, for example, the Chinese or English keyboard.

Configuring and Implementing an Input Method Subtype

  1. Implement an InputMethodExtensionAbility instance for an input method, which will be shared by all subtypes of the input method. Add metadata with the name ohos_extension.input_method to the module.json5 file to configure resource information for all subtypes.

    {
     "module": {
       // ...
       "extensionAbilities": [
         {
           "description": "InputMethodExtDemo",
           "icon": "$media:icon",
           "name": "InputMethodExtAbility",
           "srcEntry": "./ets/InputMethodExtensionAbility/InputMethodService.ts",
           "type": "inputMethod",
           "exported": true,
           "metadata": [
             {
               "name": "ohos.extension.input_method",
               "resource": "$profile:input_method_config"
             }
           ]
         }
       ]
     }
    }
    
  2. Configure the subtype fields. For details about the fields, see InputMethodSubtype. Make sure your configuration is in strict compliance with the configuration file and field specifications. For details about how to configure the locale field, see BCP 47.

    {
     "subtypes": [
       {
         "icon": "$media:icon",
         "id": "InputMethodExtAbility",
         "label": "$string:english",
         "locale": "en-US",
         "mode": "lower"
       },
       {
         "icon": "$media:icon",
         "id": "InputMethodExtAbility1",
         "label": "$string:chinese",
         "locale": "zh-CN",
         "mode": "lower"
       }
     ]
    }
    
  3. Register a listener in the input method application for subtype changes, so as to load a subtype-specific soft keyboard UI. You can also use a state variable to change the soft keyboard layout.

   import { InputMethodSubtype, inputMethodEngine } from '@kit.IMEKit';
   
   let panel: inputMethodEngine.Panel;
   let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
   inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
     let subType = inputMethodSubtype; // Save the current input method subtype. You can also change the state variable value here, based on which different layouts are displayed.
     if (inputMethodSubtype.id == 'InputMethodExtAbility') {// Different soft keyboard UIs are loaded according to the subtype.
       panel.setUiContent('pages/Index'); 
     }
     if (inputMethodSubtype.id == 'InputMethodExtAbility1') { // Different soft keyboard UIs are loaded according to the subtype.
       panel.setUiContent('pages/Index1');
     }
   });

Obtaining Information About Input Method Subtypes

  1. To obtain the current subtype of the current input method, call getCurrentInputMethodSubtype.

  2. To obtain all subtypes of the current input method, call listCurrentInputMethodSubtype.

  3. To obtain all subtypes of a specified input method, call listInputMethodSubtype.

Switching Between Input Method Subtypes

  1. To switch to another subtype of the current input method, call switchCurrentInputMethodSubtype.

  2. To switch to a specified subtype of a specified input method, call switchCurrentInputMethodAndSubtype.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙IME Kit

harmony 鸿蒙Introduction to IME Kit

harmony 鸿蒙Implementing an Input Method Application

harmony 鸿蒙Immersive Mode of the Input Method Application

harmony 鸿蒙Switching Between Input Methods

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

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

0  赞