遇到一个很奇怪的问题,flutter在自己的手机上,textfield可以正常获取焦点,但是软键盘无法正常弹出。手机型号是realMe ,系统是12。试了其他的品牌的手机,均可以正常弹出软键盘。
异常日志:W/InputMethodManager: Ignoring showSoftInput() as view=io.flutter.embedding.android.FlutterView is not served.
最终解决方案
在Flutter Android工程包的MainActivity增加如下代码
@Override
protected void onResume() {
super.onResume();
FlutterView flutterView = findViewById(FLUTTER_VIEW_ID);
flutterView.requestFocus();
}
其他
Flutter软键盘异常的问题很多,我的解决方案只是针对我遇到的报错,有些其他的原因,也会导致软键盘异常。比如未开启硬件加速,或者webview软键盘不弹出,都是其他的解决方案。
问题排查回顾
首先看了打印的异常日志:
W/InputMethodManager: Ignoring showSoftInput()
as view=io.flutter.embedding.android.FlutterView
is not served.
从日志来看,貌似显示软键盘的方法没有执行成功。
于是进入InputMethodManager查看源码:
if (!hasServedByInputMethodLocked(view)) {
Log.w(TAG, "Ignoring showSoftInput() as view=" + view + " is not served.");
return false;
}
/**
* Returns {@code true} when the given view has been served by Input Method.
*/
private boolean hasServedByInputMethodLocked(View view) {
final View servedView = getServedViewLocked();
return (servedView == view
|| (servedView != null && servedView.checkInputConnectionProxy(view)));
}
结合源码我推测,显示软键盘没有成功送达到FlutterView。我估计问题出在FlutterView,也许FlutterView 失焦了。
那么FlutterView 是什么呢?这边我就不赘述了,感兴趣的可以自己搜索一下。
首先得获取到FlutterView,
进入FlutterActivitya源码
/**
* The ID of the {@code FlutterView} created by this activity.
*
* <p>This ID can be used to lookup {@code FlutterView} in the Android view hierarchy. For more,
* see {@link android.view.View#findViewById}.
*/
public static final int FLUTTER_VIEW_ID = ViewUtils.generateViewId(0xF1F2);
可以通过FLUTTER_VIEW_ID获取FlutterView
@Override
protected void onResume() {
super.onResume();
FlutterView flutterView = findViewById(FLUTTER_VIEW_ID);
flutterView.requestFocus();
}
于是我就在想通过,主动给FlutterView 设置焦点,尝试之后,软键盘正常了。
网友评论