android监听键盘弹出or隐藏方法

1.创建自定义KeyboardListenRelativeLayout,继承自RelativeLayout

public class KeyboardListenRelativeLayout extends RelativeLayout {
 
 private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();
 
 public static final byte KEYBOARD_STATE_SHOW = -3;
 public static final byte KEYBOARD_STATE_HIDE = -2;
 public static final byte KEYBOARD_STATE_INIT = -1;
 
 //true: show;  false:hide
 private boolean lastStatus = false;
 
 private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
 
 public KeyboardListenRelativeLayout(Context context) {
  super(context);
  init();
 }
 public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }
 
 public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init();
 }
 
 public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
  this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
 }
 
 public void init()
 {
  getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

   private int screenheight = 0;
   public int getHeight()
   {
    if(screenheight>0) return screenheight;
    WindowManager wm = (WindowManager) getContext()
                     .getSystemService(Context.WINDOW_SERVICE);
  
        //int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        screenheight = height;
        return screenheight;
    
   }
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                Rect r = new Rect();
                ((Activity)getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(r); ;

                int screenHeight = getHeight();
                int heightDiff = screenHeight - (r.bottom - r.top);
                Log.d("Keyboard Size", "Size: " + heightDiff);
                boolean visible = Math.abs(heightDiff) > screenHeight / 3;
                
                if(lastStatus!=visible)
                {
                 lastStatus = visible;
                 Log.d("Keyboard", "Keyboard " + (visible?"opened":"closed"));
                 if(!visible&&onKeyboardStateChangedListener != null) {
         onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
        }
                 if(visible&&onKeyboardStateChangedListener != null) {
         onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
        }
                }
            }
        });
 }
 
 public interface IOnKeyboardStateChangedListener {
  public void onKeyboardStateChanged(int state);
 }
}

2.配置到XML

<?xml version="1.0" encoding="utf-8"?>
<com.ziines.it.views.KeyboardListenRelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:fresco="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/ll_chat_layout"
    >

3.Activity设置

mLl_chat_layout.setOnKeyboardStateChangedListener(new KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener() {
  public void onKeyboardStateChanged(int state) {
    switch (state) {
      case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE:
        NMGLog.i(TAG,"隐藏");
        break;
      case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW:
        NMGLog.i(TAG,"弹起");
        break;
      default:
        break;
    }
  }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值