我需要在我的应用程序中使用自定义输入法来处理特定的编辑文本。
输入法成功适用于所有应用程序(当我从系统菜单中选择它时)。所以这个方法都可以。
但是当我试图明确强制使用它时:
<EditText
android:id="@+id/edit"
android:inputMethod="tx.android.softkeyboard.TXSoftKeyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
它会触发ClassNotFoundException
Caused by: java.lang.ClassNotFoundException: tx.android.softkeyboard.TXSoftKeyboard
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at java.lang.Class.forName(Class.java:326)
at android.widget.TextView.<init>(TextView.java:1233)
at android.widget.EditText.<init>(EditText.java:64)
当然存在类tx.android.softkeyboard.TXSoftKeyboard。
在另一个topic用户m0skit0报告类似的行为,但没有解决方案..
我已经检查过AOSP和TextView你可以清楚地看到属性的值没有被修改(很容易遵循,在line 1034中获取属性,后来用于以下piece of code):
if (inputMethod != null) {
Class<?> c;
try {
c = Class.forName(inputMethod.toString());
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
try {
createEditorIfNeeded();
mEditor.mKeyListener = (KeyListener) c.newInstance();
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
try {
mEditor.mInputType = inputType != EditorInfo.TYPE_NULL
? inputType
: mEditor.mKeyListener.getInputType();
} catch (IncompatibleClassChangeError e) {
mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT;
}
}
所以,我们最接近解决这个问题的方法就是调用TextView#setKeyListener()
(基本上就是这段代码正在做的事情,一旦加载了mEditor.mKeyListener = (KeyListener) c.newInstance();
,就会在创建类的实例后注意到强制转换)。话虽如此,从我对这个话题的有限理解,KeyListener
不是你想要的。