軟鍵盤遮擋問題

1.gif

要實(shí)現(xiàn)如上圖效果:
注意不是將頁面整體滑上去,而是讓頁面上滑一部分,以達(dá)到不遮擋登錄按鈕的效果

實(shí)現(xiàn)思路:

  1. 監(jiān)聽鍵盤的彈起與收回
  2. 滑動(dòng)

監(jiān)聽鍵盤的彈起與收回

首先在Manifest文件中對(duì)相應(yīng)的Activity配置屬性windowSoftInputMode,讓Activity能夠在鍵盤彈起和收回時(shí)有所響應(yīng)。

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

監(jiān)聽方法:

/**
 * 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);
}

滑動(dòng)

利用view的topMargin來實(shí)現(xiàn)滑動(dòng)動(dòng)畫

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;
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容