android 实现按钮浮动在键盘上方

android 实现按钮浮动在键盘上方

大家好,我是梦辛工作室的灵,最近在帮客户修改安卓程序时,有要求到一个按钮要浮动在键盘的上方,下面大概讲一下实现方法:
其实很简单,分三步走

第一步 获取当前屏幕的高度

 Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getSize(point);
            height = point.y;

第二步 获取当前屏幕可见区域的高度,用于判断当前键盘是否隐藏或显示

public void setFloatView(View root,View floatview){
        this.root = root;	//根节点
       listener =  new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                int heightDifference = height - (r.bottom - r.top);	// 实际高度减去可视图高度即是键盘高度
                boolean isKeyboardShowing = heightDifference > height / 3;
                if(isKeyboardShowing){
                	//键盘显示 
                }else{
                	//键盘隐藏 
                }
            }
        };
        root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

第三步 当键盘隐藏时让按钮 动画移动至原有位置,当前键盘显示时让按钮动画移动至当前键盘的高度上方

        if(isKeyboardShowing){
                	//键盘显示
                    floatview.animate().translationY(-heightDifference).setDuration(0).start();
                }else{
                	//键盘隐藏
                    floatview.animate().translationY(0).start();
                }

然后我为了方便封装了一个工具类 FloatBtnUtil,很好用,下面是代码


/**
 *  梦辛灵  实现按钮浮动工具
 */
public class FloatBtnUtil {

    private static  int height = 0;
    private Activity mcontext;
    private ViewTreeObserver.OnGlobalLayoutListener listener;
    private View root;

    public FloatBtnUtil(Activity mcontext){
        this.mcontext = mcontext;
        if (height == 0){
            Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getSize(point);
            height = point.y;
        }
    }

    public void setFloatView(View root,View floatview){
        this.root = root;	//视图根节点 floatview // 需要显示在键盘上的View组件
       listener =  new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                int heightDifference = height - (r.bottom - r.top);
                boolean isKeyboardShowing = heightDifference > height / 3;
                if(isKeyboardShowing){
                    floatview.animate().translationY(-heightDifference).setDuration(0).start();
                }else{
                    floatview.animate().translationY(0).start();
                }
            }
        };
        root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

    public void clearFloatView(){
        if (listener != null && root != null)
        root.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    }

}

下面是使用代码:

    private void initFloatBtn() {
     	FloatBtnUtil  floatBtnUtil = new FloatBtnUtil(this);
        LinearLayout lin_bottom = (LinearLayout) this.findViewById(R.id.lin_bottom);
        LinearLayout lin_root = (LinearLayout)this.findViewById(R.id.lin_root);
        floatBtnUtil.setFloatView(lin_root,lin_bottom);
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灵神翁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值