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

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

分类:移动 · 发布时间:2021-10-14 18:10 · 阅读:4361

这篇文章主要介绍了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); }

总结

到此这篇关于android 实现按钮浮动在键盘上方的文章就介绍到这了,更多相关android 实现按钮浮动在键盘上方内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

标签:
android 按钮 浮动 键盘

相关文章

Android权限机制深入分析讲解

Android的权限管理遵循的是“最小特权原则”,即所有的Android应用程序都被赋予了最小权限。一个Android应用程序如果没有声明任何权限,就没有任何特权

Repo工作原理和使用介绍

Repo是谷歌用Python脚本写的调用git的一个脚本,可以实现管理多个git库。本文详细讲解了Repo的工作原理和使用介绍,需要的朋友可以收藏下,方便下次浏览观看

Android自定义双向滑动控件

这篇文章主要为大家详细介绍了Android自定义双向滑动控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Android开发教程之初识Android App

这篇文章主要为大家详细介绍了Android开发教程之初识Android App的相关资料,感兴趣的小伙伴们可以参考一下

解析iOS内存不足时的警告以及处理过程

这篇文章主要介绍了iOS内存不足时的警告以及处理过程,包括View Controller和生命周期等相关方面的知识,需要的朋友可以参考下

返回分类 返回首页