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

Android 弹出软键盘所遇到的坑及解决方法

程序员文章站 2024-02-15 19:56:52
demo下载 重要代码: //1、此layout作为最外层的layout; //2、设置需要调整的view: setadjustview(view vie...

demo下载

重要代码:

//1、此layout作为最外层的layout;
//2、设置需要调整的view: setadjustview(view view);
//3、如果需要控制输入框的显示与隐藏,可以实现oninputviewvisiblelistener接口;
public class softinputadjustlayout extends relativelayout {
  private static final string tag = softinputadjustlayout.class.getsimplename();
  private view adjustview;
  public softinputadjustlayout(context context) {
    this(context, null);
  }
  public softinputadjustlayout(context context, attributeset attrs) {
    this(context, attrs, 0);
  }
  public softinputadjustlayout(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    this.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
      @override
      public void ongloballayout() {
        try {
          rect r = new rect();
          getwindowvisibledisplayframe(r);
          int heightdiff = getrootview().getheight() - (r.bottom - r.top);
          if (heightdiff > 100) {
            if (oninputviewvisiblelistener != null) {
              oninputviewvisiblelistener.onshowinputview();
            }
          } else {
            if (oninputviewvisiblelistener != null) {
              oninputviewvisiblelistener.onhideinputview();
            }
          }
        } catch (exception e) {
          e.printstacktrace();
        }
      }
    });
  }
  /**
   * 需要调整的view
   *
   * @param adjustview
   */
  public void setadjustview(view adjustview) {
    this.adjustview = adjustview;
  }
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    if (adjustview != null) {
      rect rect = new rect();
      getwindowvisibledisplayframe(rect);
      int totalheight = getheight();
      int nowheight = rect.bottom - rect.top;
      int keyboardheight = totalheight - nowheight;
      if (keyboardheight > totalheight / 4) {
        super.onmeasure(widthmeasurespec, measurespec.makemeasurespec(totalheight, measurespec.exactly));
        layoutparams params = (layoutparams) adjustview.getlayoutparams();
        params.bottommargin = keyboardheight;
      } else {
        super.onmeasure(widthmeasurespec, heightmeasurespec);
        layoutparams params = (layoutparams) adjustview.getlayoutparams();
        params.bottommargin = 0;
      }
    } else {
      super.onmeasure(widthmeasurespec, heightmeasurespec);
    }
  }
  private oninputviewvisiblelistener oninputviewvisiblelistener;
  public void setoninputviewvisiblelistener(oninputviewvisiblelistener listener) {
    oninputviewvisiblelistener = listener;
  }
  public interface oninputviewvisiblelistener {
    void onshowinputview();
    void onhideinputview();
  }
}

备注:

全屏模式:

1、通过style或者setflags(…)来设置全屏;

2、androidmanifest.xml中activity设置:android:windowsoftinputmode=”adjustresize”;

3、此layout必须作为布局的最外层layout;

4、一定要在布局中添加一个view,如下:

 <!--此view用于获取焦点,当软件盘弹出时,根据此view来弹出,而此view在最顶部,且大小为0,所以软件盘弹出,不会把背景顶上去-->
  <view
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true" 
android:focusableintouchmode="true" />

5、通过setadjustview(view view)来设置需要调整的view;

6、在全屏模式下,输入框开始是未显示的,直到弹出软键盘,才会显示输入框。因为如果输入框一开始就显示,那软键盘就会根据输入框来弹出,且输入框在底部,那就可能造成把整个view顶上去。所以这时我们应该实现oninputviewvisiblelistener这个接口来控制输入框的显示与隐藏。

非全屏模式

1、androidmanifest.xml中activity设置:android:windowsoftinputmode=”adjustresize”;

2、此layout必须作为布局的最外层layout;

3、通过setadjustview(view view)来设置需要调整的view;

4、如果需要控制输入框的显示与隐藏,就实现oninputviewvisiblelistener这个接口。

以上所述是小编给大家介绍的android 弹出软键盘所遇到的坑及解决方法,希望对大家有所帮助