android 实现按钮浮动在键盘上方的实例代码

作者:灵神翁 时间:2022-12-21 11:32:33 

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

其实很简单,分三步走

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


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);
}

来源:https://blog.csdn.net/weixin_41392105/article/details/105010624

标签:android,按钮,浮动,键盘
0
投稿

猜你喜欢

  • SpringBoot自动装配原理详解

    2023-07-03 05:49:08
  • 浅谈java中BigDecimal的equals与compareTo的区别

    2023-09-02 07:20:22
  • 详解Java内部类与对象的打印概念和流程

    2021-10-10 21:36:56
  • 利用Jackson解决Json序列化和反序列化问题

    2023-02-16 14:59:54
  • C#基础知识之new关键字介绍

    2021-09-24 02:48:26
  • C#实现基于加减按钮形式控制系统音量及静音的方法

    2022-07-09 09:31:50
  • 浅析Spring Security登录验证流程源码

    2023-03-22 01:46:44
  • 两个例子了解java中的回调机制

    2023-07-12 21:12:46
  • Android点击EditText文本框之外任何地方隐藏键盘的解决办法

    2022-12-19 14:24:41
  • SpringMVC中使用@PathVariable绑定路由中的数组的方法

    2023-11-27 14:21:01
  • SpringBoot 内置工具类的使用

    2021-08-26 11:42:21
  • spring mvc中的@ModelAttribute注解示例介绍

    2023-10-15 07:07:06
  • Java编写实现坦克大战小游戏

    2023-11-24 09:15:34
  • Android实现美团外卖底部导航栏动画

    2022-09-21 20:09:13
  • mybatis使用pagehelper插件过程详解

    2023-02-16 18:11:11
  • springboot项目部署到宝塔的详细图文教程

    2023-03-27 05:24:31
  • 谈C# using的用法与好处

    2022-02-10 08:20:01
  • Android自定义LinearLayout布局显示不完整的解决方法

    2022-09-14 17:44:16
  • Java集合删除元素ArrayList实例详解

    2022-11-09 19:12:39
  • Android 架构之数据库框架搭建

    2021-09-28 23:26:06
  • asp之家 软件编程 m.aspxhome.com