首页 > 解决方案 > 如何在 EditText 上使用 android:inputType 来指定我的自定义 IME?

问题描述

我的应用程序包含一个自定义 IME(扩展 InputMethodService),我现在想将其指定为我的应用程序中所有 EditTexts 的 IME。

为每个 EditText指定一个android:inputMethod似乎是一种旧方法,但android:inputMethod现在已弃用。工具提示说要android:inputType改用。

我现有的一个 EditText 代码是android:inputType="numberPassword",但是当我将其更改为android:inputType=".MyCustomIME"(或android:inputType="com.example.MyCustomIME")时,我在 Android Studio 中收到此错误:

无法解析标志。

那么指定我的 IME 的正确方法是什么(对于单个 EditText 或整个应用程序) - 并且不会丢失我现有的numberPassword值(因为这决定了我将使用的 IME 子类型)?

我的 IME 在我的清单中定义为:

<service
    android:name=".MyCustomIME"
    android:label="@string/my_keyboard"
    android:permission="android.permission.BIND_INPUT_METHOD">
    <intent-filter>
        <action
            android:name="android.view.InputMethod" />
    </intent-filter>

    <meta-data
        android:name="android.view.im"
        android:resource="@xml/keyboard_method" />
</service>

(注意 -android:name="com.example.MyCustomIME"在上面的<service />标签中使用没有任何区别。)

标签: androidandroid-input-methodandroid-inputtype

解决方案


推荐阅读