編輯:關於 Android 程式設計
在 Android 應用中,虛擬鍵盤的彈起往往會遮擋住介面上的部分元件。雖然我們可以透過在清單檔(Manifest)中設定相關屬性,讓版面隨著鍵盤彈出而自動上推,但這種做法在登入頁面常常效果有限,關鍵按鈕仍可能被遮住,直接影響使用者體驗。既然問題存在,就必須想辦法解決。
先談談整體思路:我們需要取得螢幕高度、版面高度,再用螢幕高度扣除版面高度得到剩餘空間,最後將這個高度差與虛擬鍵盤的實際高度做比對,據此決定要如何調整版面位置。以下是具體的程式碼實作:
private LinearLayout logo_layout;
private ImageView iv_logo;
private int sh;
private int layoutH;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo_layout=(LinearLayout) findViewById(R.id.logo_layout);
iv_logo=(ImageView) findViewById(R.id.iv_logo);
//獲取屏幕的高度
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
windowMgr.getDefaultDisplay().getMetrics(dm);
sh = dm.heightPixels;
logo_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
// 當layout執行結束後回調此方法
@Override public void onGlobalLayout() {
logo_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
layoutH = logo_layout.getHeight();
}
});
//當鍵盤彈起的時候用屏幕的高度減去布局的高度,同時獲取到鍵盤的高度,用鍵盤的高度和剩余的高度做對比
SoftKeyBoardListener.setListener(MainActivity.this, new OnSoftKeyBoardChangeListener() {
@Override public void keyBoardShow(int height) {
//鍵盤彈起回調
int h=sh-layoutH;
if(h>height){//高度大於鍵盤的高度
setLayoutH(80);
}else{
//高度小於鍵盤的高度
int resetH=Math.abs(height+layoutH-sh);
setLayoutH(resetH);
}
}
@Override public void keyBoardHide(int height) {
//鍵盤隱藏回調
setLayoutH(80);
}
});
}
/**
* 重新設置布局高度
*/
private void setLayoutH(int h){
LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) iv_logo.getLayoutParams();
layoutParams.topMargin=dip2px(MainActivity.this, h);
iv_logo.setLayoutParams(layoutParams);
}
/**
* 根據手機的分辨率從 dp 的單位 轉成為 px(像素)
*/
public static int dip2px(Context context,float dpValue) {
final float scale =context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
接著是監聽鍵盤狀態的核心工具類。它透過監聽根視圖可視區域的變化,判斷鍵盤的顯示與隱藏,並回傳鍵盤高度:
private View rootView;//activity的根視圖
int rootViewVisibleHeight;//紀錄根視圖的顯示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//獲取activity的根視圖
rootView = activity.getWindow().getDecorView();
//監聽視圖樹中全局布局發生改變或者視圖樹中的某個視圖的可視狀態發生改變
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
//獲取當前根視圖在屏幕上顯示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根視圖顯示高度沒有變化,可以看作軟鍵盤顯示/隱藏狀態沒有改變
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根視圖顯示高度變小超過200,可以看作軟鍵盤顯示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根視圖顯示高度變大超過200,可以看作軟鍵盤隱藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
以上程式碼都附有詳細註解,方便理解每一步的作用。實際運行效果如下:

透過上述方式,登入按鈕便不再會被虛擬鍵盤遮擋,使用者操作起來更加順手。希望這份分享能對大家的開發有所幫助。
Android 輕量儲存利器:SharedPreferences 完整實戰
SharedPreferences 是 Android 提供的一種輕量儲存機制,適合用來保存一些簡單的設定資訊,例如預設歡迎語、登入的使用者名稱與密碼等。它以鍵值對的方式
Android 學習筆記(十二)——使用 Intent 傳遞資料的幾種方式
使用 Intent 傳遞資料的幾種方式 除了從 Activity 返回資料,我們也常常需要把資料傳遞給 Activity。對此,可以使用 Intent 物件將資料傳遞
從零開始學 Android:內部儲存(Internal Storage)實作教學(三十六)
把資料寫入內部儲存,基本步驟只有三步: 1. 呼叫 openFileOutput(),傳入檔名與操作模式,取得 FileOutputStream 物件。 2. 以 wri
Android AlertDialog 五種對話框完整實戰:確認、單選、多選、列表與自定義
幾乎每一款 App 都離不開對話框:刪除前的二次確認、性別選擇、興趣勾選、部門挑選,甚至是帶輸入框與圖片的「自定義彈窗」。本文用一個可直接跑起來的示例,把 AlertDi