1. 程式人生 > >android中監聽軟鍵盤的彈出與隱藏,並獲取軟鍵盤的高度

android中監聽軟鍵盤的彈出與隱藏,並獲取軟鍵盤的高度

最近專案中有一個需求上彈出軟鍵盤的時候,輸入框位移至輸入框上方,但是Activity中其他的VIew都不動。這個需求需要監聽軟軟鍵盤的彈出與隱藏,並獲取鍵盤的高度。上網找了一下發現,Android竟然沒有提供相應的介面。網上找到的替代方法中最常用的就是自定義根試圖從寫onSizeChanged方法,這是連結。但是這種方法需要設定android:windowSoftInputMode="adjustResize",就是說鍵盤彈出時候,activity的佈局會被壓縮,與我需要的彈出後其他佈局不動的需求不相同。只好自己另外想方法了。

要實現這樣的功能,需要解決兩個問題。

1.需要一個在軟鍵盤彈出的時候,必然會觸發的事件

2.判斷鍵盤是彈出還是隱藏,並獲取鍵盤高度

監聽的事件,我選擇了ViewTreeObserver中的OnGlobalLayoutListener事件,這個事件在一個檢視樹中全域性佈局發生改變或者檢視樹中的某個檢視的可視狀態發生改變時會被處罰,而軟鍵盤的彈出和隱藏很明顯會影響到根檢視的可視狀態(一部分被軟鍵盤遮擋了)。

  判斷鍵盤是彈出還是隱藏,這個只要判斷根檢視可視高度是變大還是變小就行,變化的高度就是軟鍵盤的高度了。

  下面提供實現的核心程式碼

package com.ljh.softkeyboardlistener;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by liujinhua on 15/10/25.
 */
public class SoftKeyBoardListener {
    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);
    }
}
在Activity中這樣使用
<pre name="code" class="java">    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adjust);
        SoftKeyBoardListener.setListener(AdjustUnspecifiedActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() <span style="white-space:pre">	</span>{
            @Override
            public void keyBoardShow(int height) {
                Toast.makeText(AdjustUnspecifiedActivity.this, "鍵盤顯示 高度" + height, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void keyBoardHide(int height) {
                Toast.makeText(AdjustUnspecifiedActivity.this, "鍵盤隱藏 高度" + height, Toast.LENGTH_SHORT).show();
            }
        });
    }


經過測試在adjustResize,adjustPan,adjustUnspecified下這個方法的是可用的

最後,附上我的測試程式碼下載地址