封装一个类:软键盘弹出,将控件整体顶上去

来源:互联网 发布:suse linux 扩磁盘 编辑:程序博客网 时间:2024/05/29 14:27


软键盘弹起后将整个内容框顶起:

封装一个类“CustomGlobalLayoutListener”

import android.content.Context;import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver.OnGlobalLayoutListener;/** * 实现弹出软键盘时 整个布局向上平移,解决遮挡问题 * 在onCreate中添加监听,在onDestroy中remove监听 * Created by KingsleyCheng on 2017/4/1. */public class CustomGlobalLayoutListener implements OnGlobalLayoutListener {    private Context mContext;    private View mRootView;    private View mScrollToView;    private int btnY;    /**     * @param context      context     * @param rootView     可以滚动的布局     * @param scrollToView 界面上被遮挡的位于最底部的布局(控件)     */    public CustomGlobalLayoutListener(Context context, View rootView, View scrollToView) {        this.mContext = context;        this.mRootView = rootView;        this.mScrollToView = scrollToView;    }    @Override    public void onGlobalLayout() {        Rect rect = new Rect();        // 获取root在窗体的可视区域        mRootView.getWindowVisibleDisplayFrame(rect);        int rootViewHeight = mRootView.getRootView().getHeight();        // 当前视图最外层的高度减去现在所看到的视图的最底部的y坐标        int rootInvisibleHeight = rootViewHeight - rect.bottom;        //软键盘高度        int heightDifference = rootViewHeight - (rect.bottom - rect.top);        // 若rootInvisibleHeight高度大于100,则说明当前视图上移了,说明软键盘弹出了        if (rootInvisibleHeight > 100) {            //软键盘弹出来的时候            int[] location = new int[2];            // 获取scrollToView在窗体的坐标            mScrollToView.getLocationInWindow(location);            //btnY的初始值为0,一旦赋过一次值就不再变化            if (btnY == 0) {                btnY = location[1];            }            int scrollToViewHeight = mScrollToView.getHeight();            // 计算root滚动高度,使scrollToView在可见区域的底部            int scrollHeight = (btnY + scrollToViewHeight) - rect.bottom;            //如果控件所在的当前高度大于软键盘的高度,控件就不上移            if ((rootViewHeight - location[1] - scrollToViewHeight) <        heightDifference) {                mRootView.scrollTo(0, scrollHeight);            }        } else {            // 软键盘没有弹出来的时候            mRootView.scrollTo(0, 0);        }    }}
使用方法:

private CustomGlobalLayoutListener customGlobalLayoutListener;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    requestWindowFeature(Window.FEATURE_NO_TITLE);    setContentView(R.layout.activity_create_task);    customGlobalLayoutListener = new CustomGlobalLayoutListener(this, scrollView, mBtnNextStep);    scrollView.getViewTreeObserver().addOnGlobalLayoutListener   (customGlobalLayoutListener);最后在 onDestroy()生命周期里面取消注册,防止内存溢出if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  

  • scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(customGlobalLayoutListener);  

  • else {  
  • scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(customGlobalLayoutListener);  
  • }   
  • 1 0
    原创粉丝点击
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子感冒后鼻涕特别多怎么办 经常有鼻涕怎么办才好 怀孕后鼻涕痰多怎么办 鼻炎有鼻涕痰多怎么办 宝宝咳嗽痰多鼻涕多怎么办 宝宝两岁清鼻涕咳嗽痰多怎么办 喉咙咸咸的有痰怎么办 宝宝咳嗽鼻塞喉咙有痰怎么办 绝地求生刺激战场射击键误触怎么办 在皮卡堂卡的游泳了怎么办 假如遇到老赖没能力还钱怎么办 服刑人拒不执行伤害赔偿怎么办? 面对当前严峻形势作为军人怎么办 想起诉不知道对方地址怎么办 遇见家里来嫌疑人员怎么办 老滚5老婆死了怎么办 美化包安装之后闪退怎么办 蕉下的伞坏了怎么办 苹果7通话音质特别差怎么办 雨伞的伞骨坏了怎么办 雨伞的铁丝掉了怎么办 手机银行验证码忘了怎么办 应用安装验证码忘了怎么办 大王卡激活码找不到了怎么办 信用卡的激活码找不到怎么办 育碧账号忘了怎么办 uplay八折券丢了怎么办 不小心按到了育碧解绑怎么办 台式电脑连不上网怎么办 重装系统也安装不了cad怎么办 染发灰色偏绿了怎么办 vgm数据填错了怎么办 克里格插值 不符合正态分布怎么办 克里金插值无效的输出范围怎么办 穿完臭袜子要洗手吗不洗怎么办 超敏c反应蛋白117怎么办 钢铁雄心4无模板怎么办 登录 新浪微博登录异常怎么办 微博授权失败怎么办qq uc微博授权失败怎么办 苹果手机无线网坏了怎么办