Android监听软键盘收起与弹出

在项目中有时候会需要监听软键盘的弹出与收起,并没有找到官方的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){
        //这里我们就可以拿到软键盘是否隐藏了
        }

    }
``
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,888评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,851评论 0 17
  • Activity https://developer.android.com/guide/components/a...
    XLsn0w阅读 3,994评论 0 4
  • 辞离天津,别爱人 ——2011 昨日烟雨已随风,看花落去此城静。 自知此去无年月,别留轻吻作辞行。
    雨晨I阅读 1,501评论 0 1
  • 大家一说到就业难,难免都会抱怨现在的教育质量有多差。说的人多了,好像就变成了真的。但事实是怎样的呢? 教育质量,与...
    wood3559阅读 7,172评论 0 0