最近的项目中.出现两个控件悬浮在 ScrollView上,并且ScrollView有edittextview,当edittextview获取焦点时弹出键盘的时候刚好键盘遮挡了.那两个控件,当收起键盘时,控件消失了,当时懵逼了 ,而后的解决方案是 在edittextview的下面添加一个可以空的view
<?xml version="1.0" encoding="utf-8"?> <Framlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/common_bg" android:layout_width="match_parent" android:layout_height="match_parent" app:back_img="@mipmap/approval_back" app:deep_bg="@mipmap/approval_common_bg" app:title_bg="@drawable/approval_common_box_bg" app:title_text="审批详情"> <android.support.v4.widget.NestedScrollView android:id="@+id/mList" android:layout_width="match_parent" android:layout_height="match_parent" android:tag="@string/common_bg_child_tag_inner"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="20dp" android:textColor="#462a09" android:textSize="16sp" android:textStyle="bold" tools:text="申请人 :夏利(方寸山)" /> <View android:visibility="gone" android:id="@+id/parting_line_two" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/tv_user_name" android:layout_marginTop="10dp" android:background="@color/approval_main_division_line" /> <!-- <View android:id="@+id/parting_line_two" android:layout_width="match_parent" android:layout_height="10dp" android:background="#dcbea4" />--> <ImageView android:id="@+id/iv_IsAgain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" tools:background="@mipmap/approval_mine_ok_bg_now" /> <TextView android:id="@+id/iv_level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/parting_line_two" android:layout_marginLeft="15dp" android:layout_marginTop="26dp" android:textColor="#462a09" android:textSize="14sp" tools:text="重要等级: 中" /> <!--流布局--> <org.apmem.tools.layouts.FlowLayout android:id="@+id/Rl_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_level" android:layout_marginLeft="15dp" android:layout_marginRight="30dp" /> <org.apmem.tools.layouts.FlowLayout android:id="@+id/fl_write_approval_upload_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/Rl_text" android:layout_marginLeft="20dp" android:paddingBottom="2dp" /> <!-- <View android:id="@+id/parting_line_three" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/fl_write_approval_upload_container" android:layout_marginTop="10dp" android:background="#dcbea4" />--> <View android:visibility="gone" android:id="@+id/parting_line_three" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/fl_write_approval_upload_container" android:layout_marginTop="10dp" android:background="@color/approval_main_division_line" /> <TextView android:id="@+id/tv_approval_schedule" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:layout_below="@+id/parting_line_three" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:text="审批进度" android:textColor="#462a09" android:textSize="16sp" android:textStyle="bold" /> <!--流布局--> <org.apmem.tools.layouts.FlowLayout android:id="@+id/Rl_text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_approval_schedule" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_marginRight="30dp" /> <ImageView android:visibility="gone" android:id="@+id/iv_hook_i_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Rl_text2" android:layout_marginLeft="30dp" android:background="@mipmap/approval_default_grey_hook_iv_now" /> <TextView android:visibility="gone" android:id="@+id/iv_end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Rl_text2" android:layout_marginLeft="20dp" android:layout_marginBottom="60dp" android:layout_toRightOf="@+id/iv_hook_i_one" android:text="结束" android:textColor="@color/approval_text_counts_color" /> <View android:id="@+id/parting_line_last" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/iv_end" android:layout_marginTop="10dp" android:background="@color/approval_main_division_line" android:visibility="gone" /> <EditText android:id="@+id/ed_approval" style="@style/Common_Cursor_Edit_Text" android:layout_width="match_parent" android:layout_height="160dp" android:layout_below="@id/parting_line_last" android:layout_marginTop="15dp" android:background="@null" android:gravity="left|top" android:hint="输入批阅意见" android:paddingLeft="12dp" android:paddingRight="12dp" android:textColor="@color/contacts_title_color" android:textColorHint="@color/oa_task_summzrize_input_hint_color" android:textSize="12sp" android:visibility="gone" /> <!--抵消键盘高度的View--> <View android:id="@+id/view_keyboard" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@+id/ed_approval" /> </RelativeLayout> </android.support.v4.widget.NestedScrollView> <ImageView android:id="@+id/iv_agree" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/ed_approval" android:layout_gravity="bottom|right" android:layout_marginLeft="18dp" android:layout_marginTop="20dp" android:layout_marginRight="8dp" android:src="@mipmap/iv_agree" android:tag="@string/common_bg_child_tag_inner" android:visibility="gone" /> <ImageView android:id="@+id/iv_do_not_agree" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/ed_approval" android:layout_gravity="bottom|left" android:layout_marginLeft="8dp" android:layout_marginTop="20dp" android:layout_marginRight="18dp" android:src="@mipmap/iv_do_not_agree" android:tag="@string/common_bg_child_tag_inner" android:visibility="gone" /> <ImageView android:id="@+id/iv_look" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/ed_approval" android:layout_gravity="bottom|center_horizontal" android:layout_marginTop="20dp" android:src="@mipmap/iv_agree" android:tag="@string/common_bg_child_tag_inner" android:visibility="gone" /> <ImageView android:id="@+id/iv_revoke" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="bottom|center_horizontal" android:layout_marginTop="20dp" android:background="@mipmap/approval_back_bg" android:tag="@string/common_bg_child_tag_inner" android:visibility="gone" /> </Framlayout>
1.第一步
/** * The keyboard height provider, this class uses a PopupWindow * to calculate the window height when the floating keyboard is opened and closed. */ public class KeyboardHeightProvider extends PopupWindow { /** The tag for logging purposes */ private final static String TAG = "sample_KeyboardHeightProvider"; /** The keyboard height observer */ private KeyboardHeightObserver observer; /** The cached landscape height of the keyboard */ private int keyboardLandscapeHeight; /** The cached portrait height of the keyboard */ private int keyboardPortraitHeight; /** The view that is used to calculate the keyboard height */ private View popupView; /** The parent view */ private View parentView; /** The root activity that uses this KeyboardHeightProvider */ private Activity activity; /** * Construct a new KeyboardHeightProvider * * @param activity The parent activity */ public KeyboardHeightProvider(Activity activity) { super(activity); this.activity = activity; LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); this.popupView = inflator.inflate(R.layout.popupwindow, null, false); setContentView(popupView); setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE | LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); parentView = activity.findViewById(android.R.id.content); setWidth(0); setHeight(LayoutParams.MATCH_PARENT); popupView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (popupView != null) { handleOnGlobalLayout(); } } }); } ================================================================================================== /** * 打开或关闭软键盘 * * @author zhy */ public class KeyBoardUtils { /** * 打卡软键盘 * * @param mContext 上下文 * @param mEditText 输入框 */ public static void openKeyboard(final Context mContext, final EditText mEditText) { if (null != mEditText) { if (UIUtils.isRunInMainThread()) { openKeyboardLocal(mContext, mEditText); } else { UIUtils.runInMainThread(new Runnable() { @Override public void run() { openKeyboardLocal(mContext, mEditText); } }); } } } private static void openKeyboardLocal(Context mContext, EditText mEditText) { mEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 打卡软键盘 * * @param mContext 上下文 * @param mEditText 输入框 */ public static void openKeyboard(Context mContext, View mEditText) { if (null != mEditText) { mEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } } /** * 关闭软键盘 * * @param mContext 上下文 * @param mEditText 输入框 */ public static void closeKeyboard(final Context mContext, final EditText mEditText) { if (null != mEditText) { if (UIUtils.isRunInMainThread()) { closeKeyboardLocal(mContext, mEditText); } else { UIUtils.runInMainThread(new Runnable() { @Override public void run() { closeKeyboardLocal(mContext, mEditText); } }); } } } private static void closeKeyboardLocal(Context mContext, EditText mEditText) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); mEditText.clearFocus(); } /** * 关闭软键盘 * * @param mContext 上下文 * @param mEditText 输入框 */ public static void closeKeyboard(Context mContext, View mEditText) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); mEditText.clearFocus(); } /** * 处理可能存在的输入法内存泄漏问题 * * @param destContext */ public static void fixInputMethodManagerLeak(Context destContext) { if (destContext == null) { return; } InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) { return; } String[] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"}; Field f = null; Object obj_get = null; for (int i = 0; i < arr.length; i++) { String param = arr[i]; try { f = imm.getClass().getDeclaredField(param); if (f.isAccessible() == false) { f.setAccessible(true); } // author: sodino mail:sodino@qq.com obj_get = f.get(imm); if (obj_get != null && obj_get instanceof View) { View v_get = (View) obj_get; if (v_get.getContext() == destContext) { // 被InputMethodManager持有引用的context是想要目标销毁的 f.set(imm, null); // 置空,破坏掉path to gc节点 } else { // 不是想要目标销毁的,即为又进了另一层界面了,不要处理,避免影响原逻辑,也就不用继续for循环了 // if (QLog.isColorLevel()) { // QLog.d(ReflecterHelper.class.getSimpleName(), QLog.CLR, "fixInputMethodManagerLeak break, context is not suitable, get_context=" + v_get.getContext()+" dest_context=" + destContext); // } break; } } } catch (Throwable t) { t.printStackTrace(); } } } }
/** * Start the KeyboardHeightProvider, this must be called after the onResume of the Activity. * PopupWindows are not allowed to be registered before the onResume has finished * of the Activity. */ public void start() { if (!isShowing() && parentView.getWindowToken() != null) { setBackgroundDrawable(new ColorDrawable(0)); showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0); } } /** * Close the keyboard height provider, * this provider will not be used anymore. */ public void close() { this.observer = null; dismiss(); } /** * Set the keyboard height observer to this provider. The * observer will be notified when the keyboard height has changed. * For example when the keyboard is opened or closed. * * @param observer The observer to be added to this provider. */ public void setKeyboardHeightObserver(KeyboardHeightObserver observer) { this.observer = observer; } /** * Get the screen orientation * * @return the screen orientation */ private int getScreenOrientation() { return activity.getResources().getConfiguration().orientation; } /** * Popup window itself is as big as the window of the Activity. * The keyboard can then be calculated by extracting the popup view bottom * from the activity window height. */ private void handleOnGlobalLayout() { Point screenSize = new Point(); activity.getWindowManager().getDefaultDisplay().getSize(screenSize); Rect rect = new Rect(); popupView.getWindowVisibleDisplayFrame(rect); // REMIND, you may like to change this using the fullscreen size of the phone // and also using the status bar and navigation bar heights of the phone to calculate // the keyboard height. But this worked fine on a Nexus. int orientation = getScreenOrientation(); int keyboardHeight = screenSize.y - rect.bottom; if (keyboardHeight == 0) { notifyKeyboardHeightChanged(0, orientation); } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { this.keyboardPortraitHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation); } else { this.keyboardLandscapeHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation); } } /** * */ private void notifyKeyboardHeightChanged(int height, int orientation) { if (observer != null) { observer.onKeyboardHeightChanged(height, orientation); } } }
第二步:
/** * 处理键盘的高度获取 */ private void handKeyboardHeightProvider() { keyboardHeightProvider = new KeyboardHeightProvider(this); // make sure to start the keyboard height provider after the onResume // of this activity. This is because a popup window must be initialised // and attached to the activity root view. View view = findViewById(R.id.common_bg);//最外层的布局id view.post(new Runnable() { @Override public void run() { keyboardHeightProvider.start(); } }); }
/** * {@inheritDoc} */ @Override public void onResume() { super.onResume(); keyboardHeightProvider.setKeyboardHeightObserver(this); } /** * {@inheritDoc} */ @Override public void onPause() { super.onPause(); keyboardHeightProvider.setKeyboardHeightObserver(null); KeyBoardUtils.closeKeyboard(mContext, mEdApproval);//mEdApproval 代表scrollview } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); keyboardHeightProvider.close(); }
@Override public void onKeyboardHeightChanged(int height, int orientation) { if (height == 0) { hideSystemUI();//关闭键盘隐藏可能出现的虚拟按钮 } Logger.e("键盘的高度:" + height); // 处理键盘遮挡底部留言输入框的问题 ValueAnimator anim = ValueAnimator.ofInt(0, height).setDuration(200); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mViewKeyboard.post(new Runnable() { @Override public void run() { ViewGroup.LayoutParams layoutParams = mViewKeyboard.getLayoutParams(); layoutParams.height = (int) animation.getAnimatedValue(); mViewKeyboard.requestLayout(); mList.scrollBy(0, (Integer) animation.getAnimatedValue());//滚动外层布局 } }); } }); anim.start(); }
完美解决改方法也适用控件被遮盖的地方,控件不想被遮盖就将空的view放在那个控件下面