android 软键盘遮挡布局

  上次说有时间将要写一写软键盘遮挡布局,在我实际开发中所踩过的坑.这里我将写下对于软键盘遮挡布局的问题记录我的心得.

  有些情况在布局文件中会使用EditText这个控件,所以在对EditText输入内容的时候会调用软键盘将其弹出,这时将有可能遮挡输

 入的位置,所以需要想办法将布局往上顶,让软键盘不遮挡控件.

1.当布局文件中没有WebView解决方案:

 可以在AndroidMainfest中讲对应的activity配置

android:windowSoftInputMode="adjustResize" 这个属性就可
如果在实际的开发中要取消EditText的焦点可以在其父控件中添加
android:focusable="true"
android:focusableInTouchMode="true"
这两个属性即可,从而可以通过软键盘的显示与隐藏来对EditText焦点的控制
  editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      //判断软键盘是否显示
        if (KeyBoardUtils.isKeyboardShown(holder.mLlRvItem.getRootView())) {
            holder.mEtCommitOrdermsg.setCursorVisible(true);
        } else {
            holder.mEtCommitOrdermsg.setCursorVisible(false);
        }
    }
});
判断软键盘显示与隐藏的方法请查看的我上一遍文章中有提到.
2 如果配置该属性不能解决问题的时候可以使用如下的代码解决问题:
  
public class KeyBoardToUiUp {
    public static void assistActivity (Activity activity) {
        new KeyBoardToUiUp(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private KeyBoardToUiUp(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {        
                possiblyResizeChildOfContent();    
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return r.bottom;
    }
}
只需要在布局遮挡的activity的onCreat()方法中的setContentView()的后面添加KeyBoardToUiUp.assistActivity(this);
方法即可.
备注:computeUsableHeight() 方法返回值根据fitSystemWindows的设置值来决定,如果布局中fitsSystemWindows="false", 
 return r.bottom; 如果fitsSystemWindows="true", return (r.bottom - r.top); 
3.如果是上面的方法都解决不了可以使用最后一种方法
/**
 * @param rootView 跟布局
 * @param bottomView 最后要显示的控件
  注意使用该方法的时候,在布局中需要将父布局改为ScrollView才能使用
 */
 public static void addLayoutListener(final View rootView, final View bottomView) {
    rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            rootView.getWindowVisibleDisplayFrame(rect);//获取rootView的可视区域
            int invisibleHeight = rootView.getRootView().getHeight() - rect.bottom;//获取rootView的不可视区域高度
            if (invisibleHeight > 150 ) { //键盘显示
                int[] location = new int[2];
                bottomView.getLocationInWindow(location); //获取bottomView的坐标
                int scrollHeight = (location[1] + bottomView.getHeight()) - rect.bottom;//算出需要滚动的高度
                if (scrollHeight != 0) {//防止界面元素改变调用监听,使界面上下跳动,如验证码倒计时
                    rootView.scrollTo(0, scrollHeight);
                }
            } else {
                rootView.scrollTo(0, 0);
            }
        }
    });
}
     下次将写android7.0获取手机照相机拍摄功能及将拍摄的照片加入相册和获取相册中的图片所遇到的坑,希望篇文章对你有所帮助谢谢!!!!!!!



  


  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值