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

Android输入法弹出时覆盖输入框问题的解决方法

程序员文章站 2024-02-28 14:09:10
 当一个activity中含有输入框时,我们点击输入框,会弹出输入法界面,整个界面的变化效果与manifest中对应设置的android:windowsoftin...

 当一个activity中含有输入框时,我们点击输入框,会弹出输入法界面,整个界面的变化效果与manifest中对应设置的android:windowsoftinputmode属性有关,一般可以设置的值如下,

<activity android:windowsoftinputmode=[
"stateunspecified",
"stateunchanged”, 
"statehidden",
"statealwayshidden”, 
"statevisible",
"statealwaysvisible”, 
"adjustunspecified",
"adjustresize”, 
"adjustpan"] …… >

       具体怎么设置可以查看官方文档。今天主要解决当输入法弹出时会覆盖输入框的问题。

什么情况会覆盖?

       当android的应用中如果一个activity设置了全屏属性theme.light.notittlebar.fullscreen或者设置了activity对应的主题中android:windowtranslucentstatus属性,设置方式为:<item name="android:windowtranslucentstatus">true</item>,这是如果对应的页面上含有输入框,将会导致点击输入框时软键盘弹出后键盘覆盖输入框,导致输入框看不见。

为什么?

       这其实是因为在全屏时,adjustresize属性已经失效了,该问题是系统的一个bug,参考链接。adjustresize不生效,那有没有其他方法来解决呐? 这时我们可以设置adjust属性为adjustpan属性,该属性不会失效,但是由于adjustpan会将页面整体平移,以留出输入法空间,会有一个抖动的效果,体验很差,哪有没有体验效果更好的方法呐?

解决方案:

       如果跟布局采用framelayout,则可以复写一个自定义framelayout,同时设置framelayout的android:fitssystemwindows属性为true。xml设置如下

<com.sample.ui.widget.insetframelayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitssystemwindows="true”>

       我们自定义该framelayout为insetframelayout,insetframelayout 代码如下:

public final class insetframelayout extends framelayout {
  private int[] minsets = new int[4];

  public insetframelayout(context context) {
    super(context);
  }

  public insetframelayout(context context, attributeset attrs) {
    super(context, attrs);
  }

  public insetframelayout(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
  }

  public final int[] getinsets() {
    return minsets;
  }

  @override
  protected final boolean fitsystemwindows(rect insets) {
    if (build.version.sdk_int >= build.version_codes.kitkat) {
      // intentionally do not modify the bottom inset. for some reason,
      // if the bottom inset is modified, window resizing stops working.

      minsets[0] = insets.left;
      minsets[1] = insets.top;
      minsets[2] = insets.right;

      insets.left = 0;
      insets.top = 0;
      insets.right = 0;
    }

    return super.fitsystemwindows(insets);
  }
  @override
  public final windowinsets onapplywindowinsets(windowinsets insets) {
    if (build.version.sdk_int >= build.version_codes.kitkat_watch) {
      minsets[0] = insets.getsystemwindowinsetleft();
      minsets[1] = insets.getsystemwindowinsettop();
      minsets[2] = insets.getsystemwindowinsetright();
      return super.onapplywindowinsets(insets.replacesystemwindowinsets(0, 0, 0,
          insets.getsystemwindowinsetbottom()));
    } else {
      return insets;
    }
  }

}

官方解决方案:

       官方其实也发现了问题,因此在android.support.design.internal下也重写了framelayout来解决该问题,但是该类被标记了hide。

/*
 * copyright (c) 2015 the android open source project
 *
 * licensed under the apache license, version 2.0 (the "license");
 * you may not use this file except in compliance with the license.
 * you may obtain a copy of the license at
 *
 *   http://www.apache.org/licenses/license-2.0
 *
 * unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "as is" basis,
 * without warranties or conditions of any kind, either express or implied.
 * see the license for the specific language governing permissions and
 * limitations under the license.
 */

package android.support.design.internal;

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.rect;
import android.graphics.drawable.drawable;
import android.support.annotation.nonnull;
import android.support.design.r;
import android.support.v4.view.viewcompat;
import android.support.v4.view.windowinsetscompat;
import android.util.attributeset;
import android.view.view;
import android.widget.framelayout;

/**
 * @hide
 */
public class scriminsetsframelayout extends framelayout {

  private drawable minsetforeground;

  private rect minsets;

  private rect mtemprect = new rect();

  public scriminsetsframelayout(context context) {
    this(context, null);
  }

  public scriminsetsframelayout(context context, attributeset attrs) {
    this(context, attrs, 0);
  }

  public scriminsetsframelayout(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);

    final typedarray a = context.obtainstyledattributes(attrs,
        r.styleable.scriminsetsframelayout, defstyleattr,
        r.style.widget_design_scriminsetsframelayout);
    minsetforeground = a.getdrawable(r.styleable.scriminsetsframelayout_insetforeground);
    a.recycle();
    setwillnotdraw(true); // no need to draw until the insets are adjusted

    viewcompat.setonapplywindowinsetslistener(this,
        new android.support.v4.view.onapplywindowinsetslistener() {
          @override
          public windowinsetscompat onapplywindowinsets(view v,
              windowinsetscompat insets) {
            if (null == minsets) {
              minsets = new rect();
            }
            minsets.set(insets.getsystemwindowinsetleft(),
                insets.getsystemwindowinsettop(),
                insets.getsystemwindowinsetright(),
                insets.getsystemwindowinsetbottom());
            setwillnotdraw(minsets.isempty() || minsetforeground == null);
            viewcompat.postinvalidateonanimation(scriminsetsframelayout.this);
            return insets.consumesystemwindowinsets();
          }
        });
  }

  @override
  public void draw(@nonnull canvas canvas) {
    super.draw(canvas);

    int width = getwidth();
    int height = getheight();
    if (minsets != null && minsetforeground != null) {
      int sc = canvas.save();
      canvas.translate(getscrollx(), getscrolly());

      // top
      mtemprect.set(0, 0, width, minsets.top);
      minsetforeground.setbounds(mtemprect);
      minsetforeground.draw(canvas);

      // bottom
      mtemprect.set(0, height - minsets.bottom, width, height);
      minsetforeground.setbounds(mtemprect);
      minsetforeground.draw(canvas);

      // left
      mtemprect.set(0, minsets.top, minsets.left, height - minsets.bottom);
      minsetforeground.setbounds(mtemprect);
      minsetforeground.draw(canvas);

      // right
      mtemprect.set(width - minsets.right, minsets.top, width, height - minsets.bottom);
      minsetforeground.setbounds(mtemprect);
      minsetforeground.draw(canvas);

      canvas.restoretocount(sc);
    }
  }

  @override
  protected void onattachedtowindow() {
    super.onattachedtowindow();
    if (minsetforeground != null) {
      minsetforeground.setcallback(this);
    }
  }

  @override
  protected void ondetachedfromwindow() {
    super.ondetachedfromwindow();
    if (minsetforeground != null) {
      minsetforeground.setcallback(null);
    }
  }

}

       采用如上其中的任何一种方法就可以解决输入法弹出后覆盖输入框问题。

其他问题?

       在我们使用的过程中发现有用户反馈,说只要进入我们采用该布局的页面就会崩溃,我们查看了崩溃日志,发现有部分手机都使用了相同的一个安卓系统,并且版本都是19,android4.4.x,一个被重写过的系统,该系统的代码加载方式被重写了。

为什么会崩溃?

       我们代码使用到了windowinsets,该类是api 20才提供的,因此19的系统中其实是没有该代码的,但是该系统在xml的inflate的时候就解析了该类,导致classnotfound。

新的解决方案!

       新的解决方案还是采用了上述的方式,不过会针对不同的版本写不一样的布局,分别为api 20以上与20以下提供不同的布局,这是采用系统的限定符实现的,之后20以上的原样采用上述的方式,20以下去掉onapplywindowinsets复写,这样不同的版本加载不同的代码就ok了。

 @override
  public final windowinsets onapplywindowinsets(windowinsets insets) {
    if (build.version.sdk_int >= build.version_codes.kitkat_watch) {
      minsets[0] = insets.getsystemwindowinsetleft();
      minsets[1] = insets.getsystemwindowinsettop();
      minsets[2] = insets.getsystemwindowinsetright();
      return super.onapplywindowinsets(insets.replacesystemwindowinsets(0, 0, 0,
          insets.getsystemwindowinsetbottom()));
    } else {
      return insets;
    }
  }

总结到此整个解决方案已经完成了,如过有更新的解决方案望大家分享。