欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-06 22:19:52
 背景:在写登录界面时,老板就觉得在输入密码的时候谈出来的输入法软键盘把登录按钮遮挡住了(入下图所示,不爽),连输入框都被挡了一半,于是不满意了,要叫我改,于是我...

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

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

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

1,咱们就解决问题吧。

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

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

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

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

<?xml version="1.0" encoding="utf-8"?> 
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" android:layout_height="match_parent" 
  android:fillviewport="true" 
  android:fadescrollbars="true"> 
  <linearlayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="top|center_horizontal"  
      android:orientation="vertical" 
      android:background="@color/red2" 
      android:visibility="visible"> 
      <imageview          <!--这个其实是我放布局中间的控件,我随便写的,放任何控件都可以--> 
        android:layout_width="200dp" 
        android:layout_height="160dp" 
        /> 
    </linearlayout> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:alwaysdrawnwithcache="true" 
      android:gravity="center|center_horizontal" 
      android:orientation="vertical" 
      android:visibility="visible" 
      android:background="@color/abc_search_url_text_normal"> 
      <edittext 
        android:background="@color/white" 
        android:layout_width="200dp" 
        android:layout_height="60dp" 
        /> 
    </linearlayout> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@color/cornsilk" 
      android:gravity="top|center_horizontal" 
      android:orientation="vertical" 
      android:visibility="visible"> 
      <button 
        android:layout_margintop="20dp" 
        android:gravity="center" 
        android:text="登录" 
        android:layout_width="200dp" 
        android:layout_height="50dp" /> 
    </linearlayout> 
  </linearlayout> 
</scrollview> 

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

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

<activity android:name=".view.activity.multisend.multichatactivity"      android:windowsoftinputmode="statevisible|adjustresize"/> 

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

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

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

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

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

布局看看:

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/root" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@color/white" 
        android:cliptopadding="true" 
        android:fitssystemwindows="true" 
        android:orientation="vertical"> 
  <imageview 
    android:id="@+id/logo" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_centerhorizontal="true" 
    android:layout_gravity="center" 
    android:layout_margintop="80dp" 
    android:background="@null" 
    android:scaletype="centercrop" 
    android:src="@mipmap/ic_launcher"/> 
  <scrollview 
    android:id="@+id/scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignparentleft="true" 
    android:layout_alignparentstart="true" 
    android:layout_alignparenttop="true" 
    android:layout_marginleft="15dp" 
    android:layout_marginright="15dp" 
    android:fillviewport="true" 
    android:scrollbarthumbvertical="@android:color/transparent" 
    android:scrollbars="vertical"> 
    <linearlayout 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
      <linearlayout 
        android:layout_width="match_parent" 
        android:layout_height="55dp" 
        android:layout_margintop="200dp" 
        android:gravity="center_vertical" 
        android:orientation="horizontal" 
        android:paddingleft="13dp"> 
        <imageview 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginright="15dp" 
          android:src="@drawable/ic_mobile_flag"/> 
        <edittext 
          android:id="@+id/et_mobile" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" 
          android:background="@null" 
          android:hint="请输入用户名" 
          android:inputtype="textvisiblepassword" 
          android:maxlength="11" 
          android:singleline="true" 
          android:text="" 
          android:textcolor="@color/_9" 
          android:textcolorhint="@color/_9" 
          android:textsize="14dp"/> 
        <imageview 
          android:id="@+id/iv_clean_phone" 
          android:layout_width="40dp" 
          android:layout_height="fill_parent" 
          android:scaletype="centerinside" 
          android:src="@drawable/ic_clear" 
          android:visibility="gone"/> 
      </linearlayout> 
      <view 
        android:layout_width="match_parent" 
        android:layout_height="1px" 
        android:background="@color/e"/> 
      <linearlayout 
        android:layout_width="match_parent" 
        android:layout_height="55dp" 
        android:gravity="center_vertical" 
        android:orientation="horizontal" 
        android:paddingleft="13dp"> 
        <imageview 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginright="15dp" 
          android:src="@drawable/ic_password_flag"/> 
        <edittext 
          android:id="@+id/et_password" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" 
          android:background="@null" 
          android:hint="请输入密码" 
          android:inputtype="textpassword" 
          android:maxlength="30" 
          android:singleline="true" 
          android:text="" 
          android:textcolor="@color/_9" 
          android:textcolorhint="@color/_9" 
          android:textsize="14dp"/> 
        <imageview 
          android:id="@+id/clean_password" 
          android:layout_width="40dp" 
          android:layout_height="fill_parent" 
          android:scaletype="centerinside" 
          android:src="@drawable/ic_clear" 
          android:visibility="gone"/> 
        <imageview 
          android:id="@+id/iv_show_pwd" 
          android:layout_width="40dp" 
          android:layout_height="fill_parent" 
          android:scaletype="centerinside" 
          android:src="@drawable/pass_gone"/> 
      </linearlayout> 
      <view 
        android:layout_width="match_parent" 
        android:layout_height="1px" 
        android:background="@color/e"/> 
      <button 
        android:id="@+id/btn_login" 
        android:layout_width="match_parent" 
        android:layout_height="45dp" 
        android:layout_marginbottom="10dp" 
        android:layout_margintop="21dp" 
        android:background="@drawable/bg_btn_login_selected" 
        android:text="@string/login" 
        android:textcolor="@color/white" 
        android:textsize="18dp"/> 
      <linearlayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 
        <textview 
          android:id="@+id/regist" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="right" 
          android:layout_marginbottom="10dp" 
          android:layout_weight="1" 
          android:text="注册新用户" 
          android:textcolor="#b0b8b2" 
          android:textsize="14dp"/> 
        <textview 
          android:id="@+id/forget_password" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="right" 
          android:layout_marginbottom="10dp" 
          android:layout_marginleft="21dp" 
          android:text="@string/login_forget_pwd" 
          android:textcolor="#b0b8b2" 
          android:textsize="14dp"/> 
      </linearlayout> 
    </linearlayout> 
  </scrollview> 
  <linearlayout 
    android:id="@+id/service" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignparentbottom="true" 
    android:layout_centerhorizontal="true" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:padding="10dp"> 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="联系客服" 
      android:textcolor="#b0b8b2" 
      android:textsize="14dp"/> 
    <view 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:layout_marginleft="10dp" 
      android:layout_marginright="10dp" 
      android:background="@color/e"/> 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="关于我们" 
      android:textcolor="#b0b8b2" 
      android:textsize="14dp"/> 
  </linearlayout> 
</relativelayout> 

然后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) { 
   /* old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 
   现在认为只要控件将activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起*/ 
    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); 
  } 
}); 
/** 
 * 缩小 
 * 
 * @param view 
 */ 
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(); 
} 
/** 
 * f放大 
 * 
 * @param view 
 */ 
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实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法,希望对大家有所帮助