The input method subtypes allow the input method to switch to a specific mode or language, for example, the Chinese or English keyboard.
An input method application developer only needs to register and implement one InputMethodExtensionAbility. All input method subtypes share this InputMethodExtensionAbility. Add metadata in the module.json5 configuration file, with name set to ohos.extension.input_method, to configure resource information for all subtypes.
"extensionAbilities": [
{
"srcEntry": "./ets/InputMethodExtensionAbility/InputMethodService.ets",
"name": "InputMethodService",
"label": "$string:MainAbility_label",
"description": "$string:extension_ability_descriptor",
"type": "inputMethod",
"exported": true,
"metadata": [
{
"name": "ohos.extension.input_method",
"resource": "$profile:input_method_config"
}
]
}
], Configure the subtype information based on the configuration file format and fields, and place the subtype configuration file input_method_config.json in the profile folder under the application's resource directory. For details about the fields, see InputMethodSubtype. For details about how to configure the locale field, see i18n-locale-culture.
{
"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"
}
]
} 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.
// Register a listener in the input method application for subtype changes.
inputMethodAbility.on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => {
if (inputMethodSubtype.id === 'InputMethodExtAbility') {
AppStorage.setOrCreate('subtypeChange', CustomInputMethodSubtype.english);
}
if (inputMethodSubtype.id === 'InputMethodExtAbility1') {
AppStorage.setOrCreate('subtypeChange', CustomInputMethodSubtype.chinese);
}
}); To obtain the current subtype of the current input method, call getCurrentInputMethodSubtype.
To obtain all subtypes of the current input method, call listCurrentInputMethodSubtype.
To obtain all subtypes of a specified input method, call listInputMethodSubtype.
To switch to another subtype of the current input method, call switchCurrentInputMethodSubtype.
To switch to a specified subtype of a specified input method, call switchCurrentInputMethodAndSubtype.