1. 程式人生 > >軟鍵盤彈出後,Activity變形,popupWindow遮蓋住。

軟鍵盤彈出後,Activity變形,popupWindow遮蓋住。

"adjustUnspecified" It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.

This is the default setting for the behavior of the main window.

"adjustResize" The activity's main window is always resized to make room for the soft keyboard on screen.
"adjustPan" The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

翻譯成中文:

adjustUnspecified :

沒有被指定是實現adjustResize這個模式還是實現adjustPan這個模式。系統會根據該Activity視窗是否有滾動控制元件,自動的挑選這兩個模式的一種。如果該Activity有滾動控制元件,該視窗則使用adjustResize模式,重新調整大小,通過滾動可以使視窗的所有內容在一個更小的區域全部展示出來。如果該Activity沒有滾動控制元件,則採用adjustPan模式。

如果不指定windowSoftInputMode,adjustUnspecified這個就是預設的的設定。

adjustResize:

Activity的主視窗總是調整大小,來為螢幕上的軟鍵盤騰出空間。

adjustPan:

Activity的主視窗不會調整大小來為軟鍵盤騰出空間。然而,視窗的內容自動的移動以便當前焦點永遠不會被軟鍵盤遮蓋住,這樣使用者總是可以看到他們將要打字的內容。這通常看來是不令人滿意的,因為使用者可能需要關閉軟鍵盤,才可以點選那些被軟鍵盤遮住的控制元件。

注意:adjustResize模式,在Activity全屏的時候,不會響應。

由以上的解釋,我們可以知道,如果想要背景圖不被軟鍵盤壓縮變形,則必須要使用adjustPan方法,禁止Activity的視窗重新調整大小。

如果當前Activity彈出了popWindow,當軟鍵盤彈出時,可以讓popupWindow往上移動,以便露出所有的軟鍵盤,需要這樣設定。

mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

該方法必須要在mPopupWindow.showAtLocation()方法之前呼叫。

那麼adjustResize方法在全屏的時候,不能使用,那應該怎麼做呢。國外有個人做出來了,先實現以下的類。

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // 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 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
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}


然後在setContentView方法後面,加入下面的程式碼就可以了:

AndroidBug5497Workaround.assistActivity(this);

相關推薦

鍵盤Activity變形popupWindow遮蓋

"adjustUnspecified" It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the c

鍵盤Activity變形popupWindow遮蓋

"adjustUnspecified" It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the con

KeyboardUtil【鍵盤輸入框上移一定的高度】

together true 添加 存儲 oge 底部 putty 如果 original 版權聲明:本文為HaiyuKing原創文章,轉載請註明出處! 前言 演示獲取軟鍵盤高度並保存,然後根據輸入框的原有位置是否被軟鍵盤擋住了,如果被擋住了則將整體頁面上移一定的高度,當

周記3——解決fixed屬性在ios鍵盤失效的bug

images fff 發現 apple load http none orien 兩種方法   這周在做空間(“類似”qq空間)項目。首頁是好友發表的說說,可以針對每條說說進行評論,評論框吸附固定在屏幕底部。此時,Bug來了...在ios上,軟鍵盤彈出後fixed屬性失效了

關於ListView中EditText在鍵盤的焦點問題

在ListView中,每次彈出軟鍵盤後就會重新呼叫getView()方法,導致EditText失去焦點。 解決辦法:在getView()方法中新增監聽器: editText.setOnTouchListener(new OnTouchListener() {            @Override  

Ionic3學習筆記(九)關於 Android 端鍵盤介面被壓縮的問題

本文為原創文章,轉載請標明出處 今天做了一個如下圖所示的頁面。 iOS 端毫無 bug,Android 端卻出現了問題。當軟鍵盤彈出後,Android 端的 tabs 移到了軟鍵盤的上面,再仔細一看,整個介面都被壓扁了,輸入框也不知道去哪兒了。 於是去

H5鍵盤頁面變形問題

    對於其它的標籤,不要使用top屬性,可以用外邊距margin(例如margin-top)屬性。對於背景圖片,高度不要用css設定,用js動態載入設定,就ok了; 例如:背景圖片: <img src="p.jpg" class="bj"/>.bj{wid

Android鍵盤覆蓋h5頁面輸入框問題

問題 過多 繼承 col 鍵盤 代碼 chrom 技術 fff 之前我們在使用vue進行 h5 表單錄入的過程中,遇到了Android軟鍵盤彈出,覆蓋 h5頁面 輸入框 問題,在此進行回顧並分享給大家: 系統:Android 條件:當輸入框在可視區底部或者偏下的位置 觸發

Android EditText鍵盤實現頁面標題頭不動鍵盤在編輯框下面

為了實現EditText編輯的時候彈出軟鍵盤標題頭不動,底部編輯框上移,想了好多種方法,也百度,問同事每種辦法都有問題,在這總結一下,希望能幫助到大家。 上圖看下效果: 可以看到彈出鍵盤的時候,只有EditText在軟鍵盤上面,還有一個藍色點,這個隨後再說。 用Relative

Android鍵盤介面整體上移終極解決方式

今天在應測試要求:軟鍵盤彈出不能遮擋EditView,在網上找了各種方案,基本都不行,比如: android:windowSoftInputMode=“adjustResize|stateHidden” 可能在一般的頁面是可以用吧。 下面來說一個曲線救國的方案,

Android鍵盤介面整體上移

在做搜尋功能的時候,點選搜尋框,搜尋框獲取焦點,鍵盤彈出;現在問題出來了,android軟鍵盤彈出的時候,android整個介面上移,佈局被擠壓,很難看;要解決這個問題,我們需要用到 windowSoftInputMode屬性;參考:http://blog.csdn.net

Android 鍵盤時把佈局頂上去控制元件亂套解決方法

解決辦法:方法一:在你的activity中的oncreate中setContentView之前寫上這個程式碼getWindow().setSoftInputMode(WindowManager.Lay

Android鍵盤介面整體上移的問題

AndroidManifest.xml檔案中介面對應的<activity>里加入android:windowSoftInputMode="adjustPan"   鍵盤就會覆蓋螢幕android:windowSoftInputMode="stateVisible

android 底部控制元件鍵盤跟著上移

有時候我們需要這種效果,鍵盤彈出,底部的內容跟隨上移。今天我就實現了這樣一個小小的效果 話不多說先看效果:  我們直接來看佈局檔案main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo

H5輸入框獲取焦點鍵盤會遮擋表單問題

手機內的頁面,在點選輸入框的時候,會彈出軟鍵盤。在蘋果手機上,會自動定位輸入框在螢幕上下居中(軟鍵盤和頁面頂部之間可見的螢幕)。但是安卓手機不會定位,所以要處理下安卓手機的情況。 /*監聽inpu

android開發中dialog的同時將鍵盤

宣告:此部落格為本人平時開發遇到的一些問題的整理彙總 當我們在dialog裡面添加了EditText控制元件,又需要在dialog彈出來的時候同時彈出軟鍵盤時,就需要使用Handler來實現: H

怎麽讓鍵盤部分控件上移

all mar alt color resize res 監聽 nag tcl 之前寫註冊頁面的時候,UI同學給我提了個意見,讓彈出軟鍵盤時候,左上角的標題“註冊”不動,中間內容往上移動,效果這樣經過查閱資料和多方實踐,解決方法如下 1、先要設置頁面軟鍵盤模式,這樣每次軟鍵

安卓移動端鍵盤問題解決方案

log logs 情況 sel clas scrollto refs ext 題解 移動端開發難免拿到輸入框居中或者位於底部的情況,ios 端,軟鍵盤能夠按正常預期向上滑動(這裏有個問題,當 input 聚焦時,彈出軟鍵盤,input 上滑,此時屏幕是具有滾動效果的,還沒找

解決 安卓手機 鍵盤 布局的問題

bsp doc html document 軟鍵盤 cti nbsp lur script 一、 js <script type="text/javascript">$(‘body,html‘).height(document.body.clientHeig

解決安卓手機上鍵盤擠壓背景的問題

彈出 鍵盤 color col 軟鍵盤 func class div res demo: // 解決本頁面軟鍵盤彈窗背景擠壓的問題 var clientHeight = document.documentElement.clientHeight || documen