温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android应用中程序虚拟键盘弹出遮挡登陆按钮如何解决

发布时间:2020-11-20 16:16:10 来源:亿速云 阅读:220 作者:Leah 栏目:移动开发

本篇文章给大家分享的是有关android应用中程序虚拟键盘弹出遮挡登陆按钮如何解决,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

代码实现如下;

private LinearLayout logo_layout;
  private ImageView iv_logo;
  private int sh;
  private int layoutH;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    logo_layout=(LinearLayout) findViewById(R.id.logo_layout);
    iv_logo=(ImageView) findViewById(R.id.iv_logo);
    //获取屏幕的高度
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
    windowMgr.getDefaultDisplay().getMetrics(dm);
    sh = dm.heightPixels;
    logo_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

      // 当layout执行结束后回调此方法
      @Override
      public void onGlobalLayout() {
        logo_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        layoutH = logo_layout.getHeight();
      }
    });
    //当键盘弹起的时候用屏幕的高度减去布局的高度,同时获取到键盘的高度,用键盘的高度和剩余的高度做对比
    SoftKeyBoardListener.setListener(MainActivity.this, new OnSoftKeyBoardChangeListener() {

      @Override
      public void keyBoardShow(int height) {
        //键盘弹起回调
        int h=sh-layoutH;
        if(h>height){//高度大于键盘的高度
          setLayoutH(80);
        }else{
          //高度小于键盘的高度
          int resetH=Math.abs(height+layoutH-sh);
          setLayoutH(resetH);
        }
      }

      @Override
      public void keyBoardHide(int height) {
        //键盘隐藏回调
        setLayoutH(80);
      }
    });   
  }
  /**
   * 重新设置布局高度
   */
  private void setLayoutH(int h){
    LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) iv_logo.getLayoutParams();
    layoutParams.topMargin=dip2px(MainActivity.this, h);
    iv_logo.setLayoutParams(layoutParams);
  }
  /** 
   * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
   */ 
  public static int dip2px(Context context,float dpValue) { 
    final float scale =context.getResources().getDisplayMetrics().density; 
    return (int) (dpValue * scale + 0.5f); 
  } 

private View rootView;//activity的根视图
  int rootViewVisibleHeight;//纪录根视图的显示高度
  private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

  public SoftKeyBoardListener(Activity activity) {
    //获取activity的根视图
    rootView = activity.getWindow().getDecorView();

    //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
    rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        //获取当前根视图在屏幕上显示的大小
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        int visibleHeight = r.height();
        if (rootViewVisibleHeight == 0) {
          rootViewVisibleHeight = visibleHeight;
          return;
        }

        //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
        if (rootViewVisibleHeight == visibleHeight) {
          return;
        }

        //根视图显示高度变小超过200,可以看作软键盘显示了
        if (rootViewVisibleHeight - visibleHeight > 200) {
          if (onSoftKeyBoardChangeListener != null) {
            onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
          }
          rootViewVisibleHeight = visibleHeight;
          return;
        }

        //根视图显示高度变大超过200,可以看作软键盘隐藏了
        if (visibleHeight - rootViewVisibleHeight > 200) {
          if (onSoftKeyBoardChangeListener != null) {
            onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
          }
          rootViewVisibleHeight = visibleHeight;
          return;
        }

      }
    });
  }

  private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
    this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
  }

  public interface OnSoftKeyBoardChangeListener {
    void keyBoardShow(int height);

    void keyBoardHide(int height);
  }

  public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
    SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
    softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
  }

以上做了仔细说明了,运行效果如下:

android应用中程序虚拟键盘弹出遮挡登陆按钮如何解决

以上就是android应用中程序虚拟键盘弹出遮挡登陆按钮如何解决,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI