android - 如何在 Android 上创建多个 IME(输入法编辑器)子类型?

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

我正在尝试创建多个 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>

最佳答案

似乎有一种方法可以更改 IME 子类型,即使用内置的子类型选择器,它似乎只能通过来自应用程序或 IME 的 Intent 来使用。

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/

关于android - 如何在 Android 上创建多个 IME(输入法编辑器)子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52349371/

相关文章:

android - React Native Android 模块与 Android Studio 和 React Native Builder 冲突

android - 如何在Android中使用jess

java - 当应用程序失焦(非 Activity )时响应按键事件

ios - 激活 iOS 8 自定义键盘内的文本字段时出现延迟

Android:防止软键盘在聊天窗口中关闭

java - Android为动态创建的列表设置点击事件

php - 好的http3 : writing to MySQL from Android

keyboard - Notepad++ 键盘语言刚刚更改

android - 自定义键盘的文本预测功能

themes - 安卓软键盘主题