軟鍵盤擋住輸入框問題的精確解決方案

最近給公司開發(fā)App,體驗(yàn)的人員覺得登陸界面總是被軟鍵盤擋住了不爽,自行度娘了一大堆發(fā)現(xiàn)都是基于Scrollview的或者是把界面整體拔高。但是我下面有注冊(cè)按鈕我拔高了又太丑了。
所以感覺不爽,在看了郭霖大神發(fā)的推送后根據(jù)GoogleBugs里解決方案改寫了里面的解決方案,改為了一套能夠移動(dòng)部分控件的工具,并且提高了精度。

附上鏈接:SoftKeyBoardAdapter

package com.example.hele.softkeyboardadapter;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Created by Hele on 2017/5/8.
 * 修改自http://mp.weixin.qq.com/s/sWnxIxzNkuTkUaVaPhtqLA
 */
public class SoftHideKeyBoardUtil {

    private static boolean sTranslucentStatus = false;
    private Activity mContext;
    //Root Content View
    private View mChildOfContent;
    private View mViewContainer;
    private int mUsableHeightPrevious;
    private int mLastLocation = -1;
    private int mFrameSize = -1;

    private SoftHideKeyBoardUtil(Activity activity, View container) {
        mContext = activity;
        mViewContainer = container;
        sTranslucentStatus = judgeTranslucentStatus(activity);
        //獲取根框
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        //獲取ContentView
        mChildOfContent = content.getChildAt(0);
        //ViewTreeObserver:監(jiān)聽界面繪制
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
    }

    /**
     * Call this method to prevent your view such as EditText, LoginButton or other
     * from being blocked by a soft keyboard
     *
     * @param activity               : current activity
     * @param view_container_to_move : your view group
     */
    public static void assistActivity(Activity activity, View view_container_to_move) {
        new SoftHideKeyBoardUtil(activity, view_container_to_move);
    }

    private static boolean judgeTranslucentStatus(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if ((WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS & activity.getWindow().getAttributes().flags)
                    == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) {
                return true;
            }
        }
        return false;
    }

    /**
     * Adjust the location of your container
     */
    private void possiblyResizeChildOfContent() {
        if (mLastLocation < 0) {
            mLastLocation = (int) mViewContainer.getY();
            //Use the beginning as default
            mUsableHeightPrevious = computeUsableHeight();
            mFrameSize = mUsableHeightPrevious;
        }
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != mUsableHeightPrevious) {
            int heightKeyboard = mFrameSize - usableHeightNow;
            int heightDifference = mUsableHeightPrevious - usableHeightNow;

            float adjustY = mLastLocation;

            //監(jiān)聽鍵盤變化
            if (heightKeyboard > (mFrameSize / 4) && heightDifference > (mFrameSize / 4)) { 
                //第二個(gè)條件是必須的,判斷鍵盤彈起
                //When full screen or translucentStatus is true
                int statusBar = sTranslucentStatus ? DisplayUtil.getStatusBarHeight(mContext) : 0;
                adjustY = mViewContainer.getY() - mViewContainer.getBottom() + usableHeightNow + statusBar;
            } else if (heightKeyboard == 0) {
                //收起鍵盤
            } else {
                //中英文切換
                //中文切英文 : dif < 0 . 反之, dif > 0
                adjustY = mViewContainer.getY() - heightDifference;
            }
            mViewContainer.setY(adjustY);
            mChildOfContent.requestLayout();
            mUsableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * Compute Visible Height
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

后來發(fā)現(xiàn)有人解釋過了所以我就不重新解釋一遍了。
附上鏈接: 解釋鏈接

除了possiblyResizeChildOfContent方法,其他次要內(nèi)容和Google上面的差不多。主要是添加了渲染模式的處理、中英文的切換。如果采用原來的方式用在內(nèi)部控件是無法處理中英文切換的,有興趣的話可以嘗試一下。

最后編輯于
?著作權(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)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,034評(píng)論 25 709
  • 你有才華創(chuàng)意,我有免費(fèi)攤位。華匯文創(chuàng)空間創(chuàng)意集市招募有才華的你們!? 你有沒有很多創(chuàng)意的小玩意想跟更多人分享?你是...
    華匯文創(chuàng)空間閱讀 4,603評(píng)論 0 1
  • 這半輩子顛簸 像一把雜亂的種子 隨波逐流的撒了一把在這江南的角落 但我深知只是過客 是發(fā)不了芽的 過年本是歸家的喜...
    得一生二閱讀 327評(píng)論 0 0
  • 我的媽媽是一名護(hù)士,她每天很早出門,但是很晚才回家。爸爸說,媽媽的“家”,是醫(yī)院。 我問媽媽:“媽媽,你的...
    皓皓_f9d7閱讀 658評(píng)論 0 0
  • [精進(jìn)每一天]第293天 2017.9.4 我真是幸福得不像話這句話是一本書《人生不設(shè)限》里面前言的標(biāo)題。 作者...
    梁億閱讀 507評(píng)論 0 0

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