Android EditText被软键盘遮盖处理

来源:互联网 发布:linux系统推荐 知乎 编辑:程序博客网 时间:2024/06/05 17:24

这两天android app新增了透明栏效果,结果发现键盘弹起后会遮盖屏幕底部的EditText,没有像想象中的调整窗口大小,并滚动ScrollView,将EditText显示在键盘上方。之前也遇到过类似问题,所以解决后就干脆写把所有关于EditText和键盘之间问题和解决思路都记录一下,以便以后查阅。

  一、在5.0以前,如果EditText设置了gravity=“center|right”其中之一且同时设置了singleLine=“true”,就会导致屏幕底部的EditText连续点击弹出键盘时,从第二次开会一直遮挡住EditText。

  5.0+则不会有该问题,解决办法也简单,在EditText外层包裹ScrollView,并设置键盘模式为adjustResize模式即可(两者缺一不可)。

  二、按上面描述的解决办法,一直没什么问题,直到最近加了透明栏效果,键盘模式仿佛直接失效,每次点击底部EditText时,键盘都从底部弹起,窗口大小也不会调整,底部EditText也会被遮盖。最后发现是当使用getWindow().getDecorView().setSystemUiVisibility方法设置了SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN或SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION ,或者设置了 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)时,会引发EditText被键盘遮盖问题。

  因为透明栏效果是从4.4开始支持,也就导致从4.4开始项目就存在这个BUG,当然只考虑5.0+的透明栏效果,可以直接设置状态栏颜色实现透明栏效果,我这里由于项目需要必须要从4.4开始支持,所以必须解决这个问题。

  正常情况下,系统UI会占用app一些空间,例如状态栏、键盘、导航栏等,也就是说我们的app UI不会出现在系统UI之下,但从测试结果来看,为了占用状态栏空间或全屏,设置了上面的一些属性后,就会被系统UI覆盖。

  解决方法:

监听界面容器的layout变化,当发生变化时,通过检查窗口可见区域高度,判断键盘是否弹起,如果弹起,则修改容器bottom padding,也就是手动实现adjustResize效果,给键盘留出显示空间,这样ScrollView也会自动调整大小,将EditText显示在键盘上方。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class KeyboardPatch
{
 privateActivity activity;
 privateView decorView;
 privateView contentView;
 /**
  * 构造函数
  * @param act 需要解决bug的activity
  * @param contentView 界面容器,activity中一般是R.id.content,也可能是Fragment的容器,根据个人需要传递
  * */
 publicKeyboardPatch(Activity act, View contentView)
 {
  this.activity = act;
  this.decorView = act.getWindow().getDecorView();
  this.contentView = contentView;
 }
 /**
  * 监听layout变化
  * */
 publicvoid enable()
 {
  activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
  if(Build.VERSION.SDK_INT >= 19)
  {
   decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
  }
 }
 /**
  * 取消监听
  * */
 publicvoid disable()
 {
  activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  if(Build.VERSION.SDK_INT >= 19)
  {
   decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
  }
 }
 privateViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener =new ViewTreeObserver.OnGlobalLayoutListener()
 {
  @Override
  publicvoid onGlobalLayout()
  {
   Rect r =new Rect();
   decorView.getWindowVisibleDisplayFrame(r);
   intheight = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
   intdiff = height - r.bottom;
   if(diff != 0)
   {
    if(contentView.getPaddingBottom() != diff)
    {
     contentView.setPadding(0,0, 0, diff);
    }
   }
   else
   {
    if(contentView.getPaddingBottom() != 0)
    {
     contentView.setPadding(0,0, 0,0);
    }
   }
  }
 };
}
原创粉丝点击