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

本篇文章重点为大家讲解一下通过android实现按钮浮动在键盘上方具体方法,有需要的小伙伴可以参考一下。

成都创新互联专业为企业提供曲靖网站建设、曲靖做网站、曲靖网站设计、曲靖网站制作等企业网站建设、网页设计与制作、曲靖企业网站模板建站服务,10多年曲靖做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

第一步

获取当前屏幕的高度

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 实现按钮浮动在键盘上方内容

网站标题:通过android实现按钮浮动在键盘上方
新闻来源:http://www.hantingmc.com/qtweb/news17/287067.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联