我正在尝试在焦点上编辑文本视图的活动中手动显示/隐藏软键盘,
但是在某些设备上,它显示sys/err
并导致我的应用中出现崩溃或ANR以及以下堆栈跟踪
Fatal Exception: java.lang.StackOverflowError: stack size 8MB
at android.view.inputmethod.InputConnectionWrapper.beginBatchEdit(InputConnectionWrapper.java:106)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:398)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
有时候
Fatal Exception: java.lang.StackOverflowError: stack size 8MB
at android.view.inputmethod.InputConnectionWrapper.getSelectedText(InputConnectionWrapper.java:54)
at android.view.inputmethod.InputConnectionWrapper.getSelectedText(InputConnectionWrapper.java:54)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:241)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
手动显示/隐藏软键盘的代码:
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
mUserPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
mUserPassword.setSelection(mUserPassword.getText().length());
mUserPassword.setSelection(mUserPassword.getText().length());
if (hasFocus){
mUserPassword.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
mUserPassword.setHint("");
} else {
mUserPassword.setInputType(InputType.TYPE_CLASS_TEXT);
mUserPassword.setHint(getResources()
.getString(R.string.sign_in_hint));
}
}
});
mUserPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0){
mShowPassword.setVisibility(View.VISIBLE);
} else {
mShowPassword.setVisibility(View.GONE);
}
}
});
mUserPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE){
if (validateFields(mUserPassword.getText().toString().trim())){
showWrong(false);
mPresenter.userLogin(mUserEmail,encodeToBase64(mUserPassword.getText().toString().trim()));
} else {
showWrong(true);
}
handled = true;
}
return handled;
}
});
布局代码示例:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.my.app.example.customizeviews.Lato_Bold_TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp"
android:text="@string/sign_in_header"
android:textColor="@color/header"
android:id="@+id/header"/>
<com.my.app.example.customizeviews.Lato_Regular_TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@id/header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="12dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:lineSpacingExtra="8sp"
android:text="@string/sign_in_sub_header"
android:layout_below="@+id/header"
android:textColor="@color/subHeader"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/password"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.my.app.example.customizeviews.Lato_Regular_EditTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="20"
android:id="@+id/userPassword"
android:textSize="21sp"
android:padding="5dp"
android:hint="@string/sign_in_hint"
android:textColorHint="@color/hintColor"
android:layout_marginEnd="5dp"
android:imeOptions="actionDone"
android:maxLines="1"
android:background="@color/transparent"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showPassword"
android:layout_gravity="center"
android:layout_weight="80"
android:onClick="onClick"
android:visibility="gone"
android:layout_marginBottom="-10dp"
android:layout_marginTop="-10dp"
android:src="@drawable/ic_eye_unpressed"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@+id/editTextUnderline"
android:background="@color/editTextBottomColor"
android:layout_marginTop="9dp"
android:orientation="horizontal" />
</LinearLayout>
<com.my.app.example.customizeviews.Lato_Regular_TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="12dp"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="@+id/forgotPassword"
android:text="@string/sign_in_forgot_password"
android:onClick="onClick"
android:textColor="@color/hintColor"
app:layout_constraintTop_toBottomOf="@id/password"
app:layout_constraintEnd_toEndOf="@id/password"/>
<com.my.app.example.customizeviews.Lato_Regular_TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_in_wrong_password"
android:textColor="@color/wrongText"
android:layout_margin="5dp"
android:textSize="12sp"
android:id="@+id/wrongPassword"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/password"
app:layout_constraintStart_toStartOf="@id/password"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorPrimaryDark"
android:id="@+id/signingUser"
android:onClick="onClick"
android:paddingTop="14dp"
android:paddingBottom="14dp">
<com.my.app.example.customizeviews.Lato_Bold_TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In"
android:textColor="@color/white"
android:drawableRight="@drawable/ic_menu_next"
android:layout_centerInParent="true"
android:drawablePadding="10dp"
android:textSize="20sp"/>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/back"
android:onClick="onClick"
android:src="@drawable/ic_menu_back_dark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
</android.support.constraint.ConstraintLayout>
这个设备有问题吗?如何解决这个错误?
答案 0 :(得分:0)
您还可以在XML文件中指定输入类型, 无需实现setOnFocusChangeListener侦听器。
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:hint="@string/hint_password"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
在gradle中添加以下依赖项
compile 'com.android.support:design:26.1.0'