记录一次软键盘不能调出来的问题
一、前提条件
设置其所在Activity,AndroidManifest.xml属性 android:windowSoftInputMode=“stateVisible|adjustResize”
EditText点击本身的话,据系统规则会调用起软键盘
二、操作表现
点击EditText,系统没有调用软键盘,不知为何原因?
故手动调用软键盘看能不能调用起来
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(mContentEditText, InputMethodManager.SHOW_FORCED);
发现仍然不能调用起软键盘,故Debug showSoftInput的源代码
三、追踪问题
1、只有Debug showSoftInput源代码,最终定位到
public boolean showSoftInput(View view, int flags, ResultReceiver resultReceiver) {
checkFocus();
synchronized (mH) {
if (mServedView != view && (mServedView == null
|| !mServedView.checkInputConnectionProxy(view))) {
return false;
}
try {
return mService.showSoftInput(mClient, flags, resultReceiver);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
发现传过来的view是EditView,没有任何问题,问题在mServedView != view返回为true,mServedView竟然为另外的view,故不能向下调用mService.showSoftInput(mClient, flags, resultReceiver)去启用软键盘
2、寻找mServedView的赋值位置,
private boolean checkFocusNoStartInput(boolean forceNewFocus) {
// This is called a lot, so short-circuit before locking.
if (mServedView == mNextServedView && !forceNewFocus) {
return false;
}
final ControlledInputConnectionWrapper ic;
synchronized (mH) {
if (mServedView == mNextServedView && !forceNewFocus) {
return false;
}
if (DEBUG) Log.v(TAG, "checkFocus: view=" + mServedView
+ " next=" + mNextServedView
+ " forceNewFocus=" + forceNewFocus
+ " package="
+ (mServedView != null ? mServedView.getContext().getPackageName() : "<none>"));
if (mNextServedView == null) {
finishInputLocked();
// In this case, we used to have a focused view on the window,
// but no longer do. We should make sure the input method is
// no longer shown, since it serves no purpose.
closeCurrentInput();
return false;
}
ic = mServedInputConnectionWrapper;
mServedView = mNextServedView;
mCurrentTextBoxAttribute = null;
mCompletions = null;
mServedConnecting = true;
}
if (ic != null) {
ic.finishComposingText();
}
return true;
}
最终定位于Activity中EditView的焦点失去,导致mServedView重新被赋值,mServedView = mNextServedView;
所以… …