栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法

 背景:在写登录界面时,老板就觉得在输入密码的时候谈出来的输入法软键盘把登录按钮遮挡住了(入下图所示,不爽),连输入框都被挡了一半,于是不满意了,要叫我改,于是我看QQ的登录效果,我就去研究了一下,弹出输入法整个布局上来了,终于让老板满意了。

(如上图这样,老板不满意的,呵呵)

1,咱们就解决问题吧。

     我看了很多博客和问答,很多人都说直接在在AndroidManifest.xml中给这个Activity设置 这样就好使了,这个是否在逗,整个布局向上移动并不明显,反正我的是不好使,不知道那些博主是怎么弄好使的。不过,看评论,也有很多人说不好使。那就做一个大家都好使的代码出来。先看效果。

    哈哈,大家有没有看到,连登录按钮都一起跑上去了,应该是顶上去的。老板再也不用担心登录按钮被覆盖掉了。

    那咱们就上代码啦:代码不多,全在布局上,就可以解决。   

 
 
   
     
       
     
     
       
     
     
      

  对上面就是所有视线代码了,外面一个scrollview,包含一个LinearLayout,在中间包含了三个LinearLayout,当然了包含三个什么容器控件都行,但是一定要用权重(layout_weight)来约束,这是重点,我只说了一遍,还有就是LinearLayout内部的布局尽量用wrap_content,即时要固定高度也要适当,调节调节就好了。

使用的时候很简单,就只有上面一段布局,然而根本用不着神马AndroidManifest.xml中给这个Activity设置

 

对于这段代码,是可以将底部如果有输入框(最好用frameLayout包裹),可以向上移动的,类似于QQ输入框。可以不用ScrollView而且输入框向上滚动时,整个布局不会向上滚动。

2,最后再提供一个思路,这个思路来自于“卷皮”,卷皮的登录效果,他的设计思路是,在点击EditText输入框的时候,我第一个猜测是:得到了EditText输入焦点,或者是:猜测是监听到键盘弹出的焦点之后,卷皮顶上那个背景就把它慢慢变小隐藏起来,导致下面的两个输入框滚动到顶部去了,就方便用户输入了。这个思路也很好的解决用户直接可以输入的问题。

3,目前很多项目要解决这个问题的方法就是如上面2解决方案所示的,logo逐渐缩小,然后scroll会滚动上去。

csdn这个编辑器越来越烂了,,图片都搞不上来了

布局看看:

 
 
   
   
     
       
  
  
  
       
       
       
  
  
  
  
       
       
      

然后java代码是,

private int screenHeight = 0;//屏幕高度 
private int keyHeight = 0; //软件盘弹起后所占高度 
private float scale = 0.6f; //logo缩放比例 
private int height = 0; 
private void initView() { 
  screenHeight = this.getResources().getDisplayMetrics().heightPixels; //获取屏幕高度 
  keyHeight = screenHeight / 3;//弹起高度为屏幕高度的1/3 
} 
 
mScrollView.setonTouchListener(new View.onTouchListener() { 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    return true; 
  } 
}); 
mScrollView.addonLayoutChangeListener(new ViewGroup.onLayoutChangeListener() { 
  @Override 
  public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
    
    if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) { 
      Log.e("wenzhihao", "up------>" + (oldBottom - bottom)); 
      int dist = mContent.getBottom() - bottom; 
      if (dist > 0) { 
 ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mContent, "translationY", 0.0f, -dist); 
 mAnimatorTranslateY.setDuration(300); 
 mAnimatorTranslateY.setInterpolator(new LinearInterpolator()); 
 mAnimatorTranslateY.start(); 
 RxAnimationUtils.zoomIn(mLogo, 0.6f, dist); 
      } 
    } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) { 
      Log.e("wenzhihao", "down------>" + (bottom - oldBottom)); 
      if ((mContent.getBottom() - oldBottom) > 0) { 
 ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mContent, "translationY", mContent.getTranslationY(), 0); 
 mAnimatorTranslateY.setDuration(300); 
 mAnimatorTranslateY.setInterpolator(new LinearInterpolator()); 
 mAnimatorTranslateY.start(); 
 //键盘收回后,logo恢复原来大小,位置同样回到初始位置 
 RxAnimationUtils.zoomOut(mLogo, 0.6f); 
      } 
    } 
  } 
}); 
mBtnLogin.setonClickListener(new View.onClickListener() { 
  @Override 
  public void onClick(View v) { 
    RxKeyboardUtils.hideSoftInput(mContext); 
  } 
}); 
 
public static void zoomIn(final View view, float scale, float dist) { 
  view.setPivotY(view.getHeight()); 
  view.setPivotX(view.getWidth() / 2); 
  AnimatorSet mAnimatorSet = new AnimatorSet(); 
  ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, scale); 
  ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, scale); 
  ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", 0.0f, -dist); 
  mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX); 
  mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY); 
  mAnimatorSet.setDuration(300); 
  mAnimatorSet.start(); 
} 
 
public static void zoomOut(final View view, float scale) { 
  view.setPivotY(view.getHeight()); 
  view.setPivotX(view.getWidth() / 2); 
  AnimatorSet mAnimatorSet = new AnimatorSet(); 
  ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", scale, 1.0f); 
  ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", scale, 1.0f); 
  ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), 0); 
  mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX); 
  mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY); 
  mAnimatorSet.setDuration(300); 
  mAnimatorSet.start(); 
} 

这段代码大体就是这么实现的,动态处理sroll向上滚动问题,logo动态缩小即可解决

总结

以上所述是小编给大家介绍的Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号