Android監聽軟鍵盤收起與彈出

weixin_34378969發表於2018-05-10

在專案中有時候會需要監聽軟鍵盤的彈出與收起,並沒有找到官方的API,所以根據網上的思路,自定義View來實現監聽.
並不複雜直接上程式碼吧:

   
   import android.content.Context;
   import android.graphics.Rect;
   import android.util.AttributeSet;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewTreeObserver;
   import android.widget.LinearLayout;
   
   public class MeasuredLayout extends LinearLayout {
   
       private int largestHeight;
   
       private OnKeyboardHideListener onKeyboardHideListener;
       private int heightPrevious;
       private int heightNow;
       private View mChildOfContent;
       private int usableHeightPrevious;
   
       public MeasuredLayout(Context context, View view){
           super(context);
           addView(view);
       }
   
       public MeasuredLayout(Context context, AttributeSet attrs, int layoutId) {
           super(context, attrs);
           LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           layoutInflater.inflate(layoutId, this);
           mChildOfContent=getChildAt(0);
           mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
               @Override
               public void onGlobalLayout() {
                   possiblyResizeChildOfContent();
               }
           });
       }
   
       private void possiblyResizeChildOfContent() {
           int usableHeightNow = computeUsableHeight();
           if (usableHeightNow != usableHeightPrevious) {
               int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
               int heightDifference = usableHeightSansKeyboard - usableHeightNow;
               if (heightDifference > (usableHeightSansKeyboard / 4)) {
                   // 鍵盤彈出
                   if (onKeyboardHideListener != null) {
                       onKeyboardHideListener.onKeyboardHide(false);
                   }
               } else {
                   // 鍵盤收起
                   if (onKeyboardHideListener != null) {
                       onKeyboardHideListener.onKeyboardHide(true);
                   }
               }
               usableHeightPrevious = usableHeightNow;
           }
       }
   
       private int computeUsableHeight() {
           Rect r = new Rect();
           mChildOfContent.getWindowVisibleDisplayFrame(r);
           return (r.bottom - r.top);
       }
   
     
   
       public interface OnKeyboardHideListener {
           void onKeyboardHide(boolean hide);
       }
   
       public void setOnKeyboardHideListener(OnKeyboardHideListener onKeyboardHideListener) {
           this.onKeyboardHideListener = onKeyboardHideListener;
       }
   }

使用的時候很簡單:
只需要

 public View getContentView() {
        MeasuredLayout measuredLayout = new MeasuredLayout(this, null, R.layout.activity_apply_cash);
        measuredLayout.setOnKeyboardHideListener(this);
        return measuredLayout;
    }

將該方法返回的View塞給Activity的 setContentView();方法.
並在使用監聽的Activity實現MeasuredLayout.OnKeyboardHideListener 介面並重寫方法

 @Override
    public void onKeyboardHide(boolean hide) {
        isKeyboardHide=hide;
        if(hide){
        //這裡我們就可以拿到軟鍵盤是否隱藏了
        }

    }
``

相關文章