【问题标题】:How to create multiple IME (Input Method Editor) subtypes on Android?如何在 Android 上创建多个 IME(输入法编辑器)子类型?
【发布时间】:2019-02-20 07:03:20
【问题描述】:

我正在尝试创建多个 IME 子类型,但 Android 只能识别一个。

方法.xml

<?xml version="1.0" encoding="utf-8"?>
<input-method
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:supportsSwitchingToNextInputMethod="true"
    android:settingsActivity="com.example.softkeyboard.Settings">


    <subtype android:name="@string/display_name_english_keyboard_dynamic_ime"
        android:imeSubtypeLocale="en_US"
        android:imeSubtypeMode="keyboard"
        android:imeSubtypeExtraValue="charDataFile=strokemaps_dynamic" />

    <subtype android:name="@string/display_name_english_keyboard_ime"
        android:imeSubtypeLocale="en_US"
        android:imeSubtypeMode="keyboard"
        android:imeSubtypeExtraValue="charDataFile=strokemaps" />

</input-method>

strings.xml 中有每个名称的值。

<resources>
<string name="app_name">KK1</string>
<string name="display_name_english_keyboard_ime">English</string>
<string name="display_name_english_keyboard_dynamic_ime">English Dynamic</string>

我的 InputMethodService.onStartInputView 方法包括:

@Override
public void onStartInputView(EditorInfo ei, boolean restarting) {

    super.onStartInputView(ei, restarting);

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    List<InputMethodInfo> imil = imm.getEnabledInputMethodList();
    for (InputMethodInfo imi: imil) {
        Log.e("osiv", "input method info: "+imi.toString());
    }

    List<InputMethodSubtype> imsl = imm.getEnabledInputMethodSubtypeList(imil.get(0), true);

    for (InputMethodSubtype ims: imsl) {
        Log.e("osiv", "input method subtype: "+ims.toString());
    }

并且列出的 InputMethodInfos 包括我的 IME,但子类型列表仅包括一个子类型。如果文件中只有一个子类型,则每个子类型都会起作用。

Android 8.0 设备的语言/键盘配置选项中没有子类型,只有 IME 本身,因此无法单独启用或禁用子类型。

是否需要其他配置项来告诉 Android 允许多个 IME 子类型?

上面的代码有明显的问题吗?

这是 AndroidManifest,以防万一。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.k.k.kk1">

<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".KKInputMethodService"
        android:permission="android.permission.BIND_INPUT_METHOD"
        android:label="KK"
        android:configChanges="orientation">
        <intent-filter>
            <action android:name="android.view.InputMethod"/>
        </intent-filter>
        <meta-data
            android:name="android.view.im"
            android:resource="@xml/method"/>

    </service>

</application>

【问题讨论】:

  • 我怀疑原因是您的两个子类型具有相同的imeSubtypeLocale

标签: android keyboard android-softkeyboard android-8.0-oreo ime


【解决方案1】:

似乎有一种方法可以更改 IME 子类型,即使用内置子类型选择器,该选择器似乎只能通过应用程序或 IME 的意图获得。

final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imId);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_TITLE, “Select Enabled Subtypes”);
context.startActivity(intent);

如果您希望从 InputMethodService 中启动 Intent,则需要 FLAG。

您可以从 inputMethodInfo 对象中获取输入法 ID 'imId',使用:

String imId = inputMethodInfo.getId(); 

或者您可以使用以下方法获取 id:

String imId = Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

这个答案的核心来自: https://blog.swiftkey.com/tech-blog-android-input-method-subtypes/

【讨论】:

  • 如何在您的活动中使用该自定义 InputMethodService 类?
  • Rishav 如果我没记错的话,该活动与应用程序相关联。自定义 IMS 类由用户安装和启用,并且可以与任何应用程序一起使用。 Activity 和 IMS 之间没有排他性的联系。
【解决方案2】:

您还可以通过检索以下设置“selected_input_method_subtype”来获取系统编辑器的输入法子类型ID(当前设置编辑器或当前设置为默认编辑器,不确定)。 例如,1891618174 是 Gboard 的子类型 ID。

【讨论】:

    猜你喜欢
    • 2023-02-22
    • 1970-01-01
    • 2015-09-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多