当按如下方式定义EditText
时,我对输入类型 text
EditText
有问题:
<EditText
android:id="@+id/text_name"
android:inputType="text"
android:text="some txt"
.
.
./>
这里的inputType
是text
,文本值是"some txt"
,其中包含一个拼写错误的单词"txt"。此 EditText 包含在显示为弹出窗口的布局中。
现在,当某些Button
在Activity
中单击并弹出此布局时,单词 txt 被下划线下划线为错误的单词,并且当聚焦在EditText
中的任何其他单词时,键盘正常出现并且没有发生任何错误,但是当聚焦在拼写错误的单词 txt 中时,应用程序崩溃并出现以下异常:
11-09 16:50:02.126: W/dalvikvm(5205): threadid=1: thread exiting with uncaught exception (group=0x40ffc9a8)
11-09 16:50:02.127: W/System.err(5205): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41656d18 is not valid; is your activity running?
11-09 16:50:02.127: W/System.err(5205): at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
11-09 16:50:02.127: W/System.err(5205): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-09 16:50:02.127: W/System.err(5205): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-09 16:50:02.127: W/System.err(5205): at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
11-09 16:50:02.127: W/System.err(5205): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:847)
11-09 16:50:02.128: W/System.err(5205): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:811)
11-09 16:50:02.128: W/System.err(5205): at android.widget.Editor$PinnedPopupWindow.updatePosition(Editor.java:2207)
11-09 16:50:02.128: W/System.err(5205): at android.widget.Editor$PinnedPopupWindow.show(Editor.java:2164)
11-09 16:50:02.128: W/System.err(5205): at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:2406)
11-09 16:50:02.128: W/System.err(5205): at android.widget.Editor.showSuggestions(Editor.java:1700)
11-09 16:50:02.128: W/System.err(5205): at android.widget.Editor$1.run(Editor.java:1599)
11-09 16:50:02.128: W/System.err(5205): at android.os.Handler.handleCallback(Handler.java:725)
11-09 16:50:02.129: W/System.err(5205): at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 16:50:02.129: W/System.err(5205): at android.os.Looper.loop(Looper.java:153)
11-09 16:50:02.129: W/System.err(5205): at android.app.ActivityThread.main(ActivityThread.java:5299)
11-09 16:50:02.129: W/System.err(5205): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:50:02.130: W/System.err(5205): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:50:02.133: W/System.err(5205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-09 16:50:02.134: W/System.err(5205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-09 16:50:02.134: W/System.err(5205): at dalvik.system.NativeStart.main(Native Method)
要修复此错误,我必须将EditText
中的inputType
更改为 textNoSuggestions
.
那么,为什么会出现这个问题?以及如何使用inputType
作为text
并毫无问题地处理拼写错误的单词?
这就是我以编程方式处理它的方式:
private static PopupWindow pw;
private View layout;
private static ViewGroup vg = (ViewGroup) findViewById(R.id.popup_container);
private void initiate_popup(){
layout = inflater.inflate(R.layout.popup,
vg);
pw = new PopupWindow(layout, 500,
450, true);
pw.setOutsideTouchable(true);
pw.setBackgroundDrawable(new ColorDrawable(
android.graphics.Color.TRANSPARENT));
pw.setTouchInterceptor(on_outside_touch);
pw.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
hideKeyboard();
}
});
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
String name = "some txt";
// Declaring EditText
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
final EditText text_name = (EditText) layout.findViewById(R.id.text_name);
imm.hideSoftInputFromWindow(text_name.getWindowToken(), 0);
text_name.setText(name);
}
// hide Keyboard method
public void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) ctx
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
} else {
ctx.getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
我通过更改文本的inputType
来解决此问题,而不是text
将其更改为textNoSuggestions
,这甚至不会查看输入的单词是否正确,因此应用程序在聚焦时不会崩溃。
因此,文本将定义为:
<EditText
android:id="@+id/text_name"
android:inputType="textNoSuggestions"
android:text="some txt"
.
.
./>