編輯:關於 Android 程式設計
前言
開發做得久了,總免不了踩各種坑。而在 Android 開發路上,「軟鍵盤擋住輸入框」堪稱曠日持久的巨坑——我們慢慢來拆。

最常見的情況:頁面底部有一個 EditText,若不做任何處理,軟鍵盤彈出時就可能把它擋住。這種情況處理起來很簡單,只要在 AndroidManifest 中為該 activity 設置 android:windowSoftInputMode 為 adjustPan 或 adjustResize 即可:
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>
兩者的效果略有區別:adjustPan 是把整個界面上平移,讓輸入框露出,不改變佈局;adjustResize 則是重新計算彈出鍵盤後的界面大小,用更小的區域顯示內容,輸入框自然也在其中。基本上所有 Android 工程師都能搞定這一步。
上面是由原生 EditText 觸發鍵盤。但在 H5、Hybrid 幾乎成為 App 標配的今天,我們更常遇到的是——鍵盤由 WebView 中的網頁元素觸發。這時情況就複雜了:
首先,非全屏模式下給 activity 設置 adjustPan 會失效;其次,全屏模式下 adjustPan 和 adjustResize 都會失效。這裡的「全屏模式」指頁面全屏,包括使用 Fullscreen 主題、狀態欄著色、沉浸式狀態欄、Immersive Mode 等——只要 App 自己接管了狀態欄控制,就會出現這種問題。

上面表格的情況並非 Google 所期望,理應都能正常生效才對,所以這其實是 Android 系統本身的 BUG——也就是著名的「issue 5497」。它從 Android 1.x 時代(2009 年)就被報告,一直到 Android 7.0(2016 年)仍未修復。這不僅是個坑,還是官方親手挖的坑。
不管坑是誰挖的,最終都得開發者來填。遇坑有兩種過法:躲,或者填。
出現坑的條件是:帶 WebView 的 activity 使用了全屏模式或 adjustPan 模式。那麼躲坑就很簡單——只要 activity 中有 WebView,就不要用全屏模式,並把 windowSoftInputMode 設為 adjustResize 就好。

但總有些時候需要全屏與 WebView 兼得,這時就得換個填坑的姿勢了。幸好開發者的智慧無窮,這個坑出現多年,還是有人找到了方案。
公認最好的方案是 AndroidBug5497Workaround,只需一個神奇的類。使用步驟超簡單:
1. 把 AndroidBug5497Workaround 類複製到專案裡;
2. 在需要填坑的 activity 的 onCreate 中加一句 AndroidBug5497Workaround.assistActivity(this)。
經測試,它在各個 Android 版本上基本可用,效果與設置 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);// 全屏模式下: return r.bottom } }
代碼大致做了四件事:
1. 找到 activity 的根 View
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0);
其中 android.R.id.content 是所有 Activity 界面上開發者可控區域的根 View。全屏模式下它佔滿整個屏幕,非全屏模式下則佔滿除狀態欄以外的區域。我們常用的 setContentView 就是把指定的 View 放進它裡面,成為其子 View。所以第二行 content.getChildAt(0) 拿到的 mChildOfContent,正是我們 setContentView 放進去的 View。
2. 設置 Listener 監聽 View 樹變化
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener({ //簡化了寫法 possiblyResizeChildOfContent(); });
getViewTreeObserver() 能取得一個專門監聽 View 樹變化的觀察者。這裡註冊的 addOnGlobalLayoutListener,會在 View 樹的全局布局變化、或其中 View 可視狀態變化時回調通知——而「軟鍵盤彈出」正是觸發這個事件的一個源頭。
3. 界面變化後獲取「可用高度」
private int computeUsableHeight() { Rect rect = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(rect); // rect.top其實是狀態欄的高度,如果是全屏主題,直接 return rect.bottom就可以了 return (rect.bottom - rect.top); }
getWindowVisibleDisplayFrame(Rect rect) 能取得界面除去標題欄、除去被鍵盤遮擋部分後剩下的矩形區域。由此可見:rect.top 就是標題欄高度;屏幕高度減 rect.bottom 就是軟鍵盤高度。於是:
全屏模式下,可用高度 = rect.bottom;
非全屏模式,可用高度 = rect.bottom - rect.top。
Rect 區域示意圖

4. 最後一步,重設高度
我們算出的可用高度是目前視覺上能看到的高度,而界面實際高度還多出一個鍵盤的距離。所以最後一步就是把界面高度置為可用高度:
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; } }
代碼裡加了「heightDifference > (usableHeightSansKeyboard/4)」的判斷,是為了過濾無謂干擾。因為能觸發 OnGlobalLayout 的原因很多,不止鍵盤彈出,還包括子 View 的隱藏顯示變化等,而它們對高度的影響有限。加上這個判斷後,只有高度變化超過 1/4 屏幕才會重設,基本能保證只響應鍵盤彈出。
歸納起來就是:
普通 Activity(不帶 WebView):直接用 adjustPan 或 adjustResize;
如果帶 WebView:
a) 非全屏模式,可用 adjustResize;
b) 全屏模式,則用 AndroidBug5497Workaround 處理。
以上就是軟鍵盤遮擋輸入框的終極解法,希望對大家有所幫助。
Android 屏幕適配完整攻略:從核心概念到官方最佳實踐
屏幕適配是 Android 開發者共同的痛。本文以 Google 官方文檔為骨架,把「為什麼要適配、需要理解哪些概念、有哪些解法、實務上怎麼做」四件事講透。 一、碎片化
Android 用 setDataAndType 打開各種檔案:MIME 對照表與 Intent 範例大全
在 Android 中要用系統現有的應用打開某個檔案,關鍵是「正確的 MIME 類型」。只要透過 Intent.ACTION_VIEW 搭配 setDataAndType
MSM8909 + Android 5.1.1 的 USB 連接方式完整整理
以下測試環境以 Windows 7 電腦為主。 先給結論: 作為 Host 使用時,接上滑鼠與 U 盤都能正常工作。 作為 Client 使用時,在 Windows 7
Android RSA 加解密實踐:填充方式、分段加密與效率對比
前陣子和同事聊到數據安全傳輸,順手把之前用過的 RSA 非對稱加密方案整理成文。 RSA 是什麼 RSA 是最流行的公鑰密碼算法,密鑰長度可變,也是第一個同時支持數據加密