Android 弹出软键盘所遇到的坑及解决方法
demo下载
重要代码:
//1、此layout作为最外层的layout; //2、设置需要调整的view: setAdjustView(View view); //3、如果需要控制输入框的显示与隐藏,可以实现OnInputViewVisibleListener接口; public class SoftInputAdjustLayout extends RelativeLayout { private static final String TAG = SoftInputAdjustLayout.class.getSimpleName(); private View adjustView; public SoftInputAdjustLayout(Context context) { this(context, null); } public SoftInputAdjustLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SoftInputAdjustLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { try { Rect r = new Rect(); getWindowVisibleDisplayFrame(r); int heightDiff = getRootView().getHeight() - (r.bottom - r.top); if (heightDiff > 100) { if (onInputViewVisibleListener != null) { onInputViewVisibleListener.onShowInputView(); } } else { if (onInputViewVisibleListener != null) { onInputViewVisibleListener.onHideInputView(); } } } catch (Exception e) { e.printStackTrace(); } } }); } /** * 需要调整的view * * @param adjustView */ public void setAdjustView(View adjustView) { this.adjustView = adjustView; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (adjustView != null) { Rect rect = new Rect(); getWindowVisibleDisplayFrame(rect); int totalHeight = getHeight(); int nowHeight = rect.bottom - rect.top; int keyBoardHeight = totalHeight - nowHeight; if (keyBoardHeight > totalHeight / 4) { super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY)); LayoutParams params = (LayoutParams) adjustView.getLayoutParams(); params.bottomMargin = keyBoardHeight; } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); LayoutParams params = (LayoutParams) adjustView.getLayoutParams(); params.bottomMargin = 0; } } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } private OnInputViewVisibleListener onInputViewVisibleListener; public void setOnInputViewVisibleListener(OnInputViewVisibleListener listener) { onInputViewVisibleListener = listener; } public interface OnInputViewVisibleListener { void onShowInputView(); void onHideInputView(); } }
备注:
全屏模式:
1、通过style或者setFlags(…)来设置全屏;
2、AndroidManifest.xml中activity设置:android:windowSoftInputMode=”adjustResize”;
3、此layout必须作为布局的最外层layout;
4、一定要在布局中添加一个view,如下:
<!--此view用于获取焦点,当软件盘弹出时,根据此view来弹出,而此view在最顶部,且大小为0,所以软件盘弹出,不会把背景顶上去--> <View android:layout_width="0px" android:layout_height="0px" android:focusable="true" android:focusableInTouchMode="true" />
5、通过setAdjustView(View view)来设置需要调整的view;
6、在全屏模式下,输入框开始是未显示的,直到弹出软键盘,才会显示输入框。因为如果输入框一开始就显示,那软键盘就会根据输入框来弹出,且输入框在底部,那就可能造成把整个view顶上去。所以这时我们应该实现OnInputViewVisibleListener这个接口来控制输入框的显示与隐藏。
非全屏模式
1、AndroidManifest.xml中activity设置:android:windowSoftInputMode=”adjustResize
”;
2、此layout必须作为布局的最外层layout;
3、通过setAdjustView(View view)
来设置需要调整的view;
4、如果需要控制输入框的显示与隐藏,就实现OnInputViewVisibleListener这个接口。
以上所述是小编给大家介绍的Android 弹出软键盘所遇到的坑及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
赞 (0)