软键盘遮挡问题

软键盘遮挡问题

Android小彩虹2021-07-18 7:43:08240A+A-

1.gif

要实现如上图效果: 注意不是将页面整体滑上去,而是让页面上滑一部分,以达到不遮挡登录按钮的效果

实现思路:

  1. 监听键盘的弹起与收回
  2. 滑动

监听键盘的弹起与收回

首先在Manifest文件中对相应的Activity配置属性windowSoftInputMode,让Activity能够在键盘弹起和收回时有所响应。

<activity
    android:name=".ui.activitys.login.LoginPwdActivity"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden|adjustPan" />

监听方法:

/**
 * Register soft input changed listener.
 *
 * @param activity The activity.
 * @param listener The soft input changed listener.
 */
public static void registerSoftInputChangedListener(final Activity activity,
                                                    final OnSoftInputChangedListener listener) {
    final int flags = activity.getWindow().getAttributes().flags;
    if ((flags & WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) != 0) {
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    final View contentView = activity.findViewById(android.R.id.content);
    sContentViewInvisibleHeightPre = getContentViewInvisibleHeight(activity);
    onSoftInputChangedListener = listener;
    onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (onSoftInputChangedListener != null) {
                int height = getContentViewInvisibleHeight(activity);
                if (sContentViewInvisibleHeightPre != height) {
                    onSoftInputChangedListener.onSoftInputChanged(height);
                    sContentViewInvisibleHeightPre = height;
                }
            }
        }
    };
    contentView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}

private static int getContentViewInvisibleHeight(final Activity activity) {
    final View contentView = activity.findViewById(android.R.id.content);
    final Rect outRect = new Rect();
    contentView.getWindowVisibleDisplayFrame(outRect);
    return contentView.getBottom() - outRect.bottom;
}

/**
 * Register soft input changed listener.
 *
 * @param activity The activity.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void unregisterSoftInputChangedListener(final Activity activity) {
    final View contentView = activity.findViewById(android.R.id.content);
    contentView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
    onSoftInputChangedListener = null;
    onGlobalLayoutListener = null;
}

public interface OnSoftInputChangedListener {
    void onSoftInputChanged(int height);
}

滑动

利用view的topMargin来实现滑动动画

final ViewWrapper wrapper = new ViewWrapper(scrollview);
        SoftInputUtil.registerSoftInputChangedListener(this,
                new SoftInputUtil.OnSoftInputChangedListener() {
                    @Override
                    public void onSoftInputChanged(int height) {
                        HhixLog.e(TAG, SoftInputUtil.isSoftInputVisible(LoginPwdActivity.this) + ",height:" + height + ",tip bottom:" + tvTips.getBottom());
                        versionTv.setVisibility(SoftInputUtil.isSoftInputVisible(LoginPwdActivity.this) ? View.GONE : View.VISIBLE);
                        if (SoftInputUtil.isSoftInputVisible(LoginPwdActivity.this)) {
                            distance = height - (IMApplication.getInstance().screenSize.screenHeight - tipsBottom - UIUtils.dip2px(IMApplication.getInstance(), 24));
                            if (distance > 0) {
                                ivWelcome.setVisibility(View.INVISIBLE);
                                ObjectAnimator animator = ObjectAnimator.ofInt(wrapper, "topMargin", 0, -distance);
                                animator.setDuration(200).start();
                            }
                        } else {
                            if (distance > 0) {
                                ivWelcome.setVisibility(View.VISIBLE);
                                ObjectAnimator animator = ObjectAnimator.ofInt(wrapper, "topMargin", -distance, 0);
                                animator.setDuration(200).start();
                            }
                        }
                    }
                });

/** * Return whether soft input is visible. * <p>The minimum height is 200</p> * * @param activity The activity. * @return {@code true}: yes<br>{@code false}: no */
public static boolean isSoftInputVisible(final Activity activity) {
    return isSoftInputVisible(activity, 200);
}

/** * Return whether soft input is visible. * * @param activity The activity. * @param minHeightOfSoftInput The minimum height of soft input. * @return {@code true}: yes<br>{@code false}: no */
public static boolean isSoftInputVisible(final Activity activity, final int minHeightOfSoftInput) {
    return getContentViewInvisibleHeight(activity) >= minHeightOfSoftInput;
}

class ViewWrapper {

    private View mTarget;
    private FrameLayout.LayoutParams mParams;

    public ViewWrapper(View target) {
        mTarget = target;
        mParams = (FrameLayout.LayoutParams) mTarget.getLayoutParams();
    }

    public void setTopMargin(int topMargin) {
        mParams.setMargins(0, topMargin, 0, 0);
        mTarget.setLayoutParams(mParams);
        mTarget.requestLayout();
    }

    public int getTopMargin() {
        return mParams.topMargin;
    }

}

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1

联系我们