Android webview input標(biāo)簽 軟鍵盤(pán)遮擋問(wèn)題

在App中使用Webview中的網(wǎng)頁(yè)使用了input標(biāo)簽,用于選擇文件并且上傳;

問(wèn)題:

點(diǎn)擊input標(biāo)簽 后軟鍵盤(pán)出現(xiàn),但是整體布局未向上移動(dòng),造成軟鍵盤(pán)遮擋put標(biāo)簽等。

解決方案:

參看:stackoverflow 問(wèn)題

簡(jiǎn)單來(lái)說(shuō)就是使用AndroidBug5497Workaround 類(lèi),在onCreate時(shí)設(shè)置

虛擬按鍵出現(xiàn)問(wèn)題

設(shè)備:紅米redmi 5 plus

類(lèi)似紅米5 plus 這樣的設(shè)備底部可能有虛擬按鍵,并且加載的網(wǎng)頁(yè)十分特殊,網(wǎng)頁(yè)底部有一個(gè)pop,如圖

image

在使用AndroidBug5497Workaround 出現(xiàn)pop(紅框部分)被底部的虛擬鍵(藍(lán)框部分)遮擋。

解決方案

 * 為修復(fù)WebView內(nèi)的input 彈出軟鍵盤(pán)導(dǎo)致布局被遮擋引入
 * https://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#19494006
 */
public class AndroidBug5497Workaround {
    // For more information, see https://issuetracker.google.com/issues/36911528
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private int frameLayoutHeight = 0;

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutHeight = frameLayoutParams.height;// ?。?!修改前保存原有高度
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                if(0 != frameLayoutHeight) {
                    frameLayoutParams.height = frameLayoutHeight;// !??!收起鍵盤(pán)恢復(fù)原有高度
                }
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}```
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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