Android監(jiān)聽軟鍵盤收起與彈出

在項(xiàng)目中有時(shí)候會(huì)需要監(jiān)聽軟鍵盤的彈出與收起,并沒有找到官方的API,所以根據(jù)網(wǎng)上的思路,自定義View來實(shí)現(xiàn)監(jiān)聽.
并不復(fù)雜直接上代碼吧:

   
   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;
       }
   }

使用的時(shí)候很簡(jiǎn)單:
只需要

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

將該方法返回的View塞給Activity的 setContentView();方法.
并在使用監(jiān)聽的Activity實(shí)現(xiàn)MeasuredLayout.OnKeyboardHideListener 接口并重寫方法

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

    }
``
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,167評(píng)論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,358評(píng)論 0 17
  • Activity https://developer.android.com/guide/components/a...
    XLsn0w閱讀 773評(píng)論 0 4
  • 辭離天津,別愛人 ——2011 昨日煙雨已隨風(fēng),看花落去此城靜。 自知此去無年月,別留輕吻作辭行。
    雨晨I閱讀 245評(píng)論 0 1
  • 大家一說到就業(yè)難,難免都會(huì)抱怨現(xiàn)在的教育質(zhì)量有多差。說的人多了,好像就變成了真的。但事實(shí)是怎樣的呢? 教育質(zhì)量,與...
    wood3559閱讀 2,563評(píng)論 0 0

友情鏈接更多精彩內(nèi)容