android 键盘监听

从知乎上看到的一种解决方案,目前还没发现有什么坑。

public class SoftKeyBroadManager implements ViewTreeObserver.OnGlobalLayoutListener{        public interface SoftKeyboardStateListener {        void onSoftKeyboardOpened(int keyboardHeightInPx);        void onSoftKeyboardClosed();    }    private final List listeners = new LinkedList();    private final View activityRootView;    private int lastSoftKeyboardHeightInPx;    private boolean isSoftKeyboardOpened;    public SoftKeyBroadManager(View activityRootView) {        this(activityRootView,false);    }    public SoftKeyBroadManager(View activityRootView, boolean isSoftKeyboardOpened) {        this.activityRootView = activityRootView;        this.isSoftKeyboardOpened = isSoftKeyboardOpened;        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);    }    @Override    public void onGlobalLayout() {        final Rect r = new Rect();        activityRootView.getWindowVisibleDisplayFrame(r);        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);               if (!isSoftKeyboardOpened && heightDiff > 500) {             // 如果高度超过500 键盘可能被打开            isSoftKeyboardOpened = true;            notifyOnSoftKeyboardOpened(heightDiff);        } else if (isSoftKeyboardOpened && heightDiff < 500) {            isSoftKeyboardOpened = false;            notifyOnSoftKeyboardClosed();        }    }    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {        this.isSoftKeyboardOpened = isSoftKeyboardOpened;    }    public boolean isSoftKeyboardOpened() {        return isSoftKeyboardOpened;    }      public int getLastSoftKeyboardHeightInPx() {        return lastSoftKeyboardHeightInPx;    }    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.add(listener);    }    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.remove(listener);    }    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardOpened(keyboardHeightInPx);            }        }    }    private void notifyOnSoftKeyboardClosed() {        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardClosed();            }        }    }}

使用方法

softKeyBroadManager = new SoftKeyBroadManager(root);    softKeyBroadManager.addSoftKeyboardStateListener(softKeyboardStateListener);private SoftKeyBroadManager.SoftKeyboardStateListener softKeyboardStateListener = new            SoftKeyBroadManager.SoftKeyboardStateListener() {                @Override                public void onSoftKeyboardOpened(int keyboardHeightInPx) {                    PandaLogUtils.getInstance().i("键盘打开");                }                @Override                public void onSoftKeyboardClosed() {                    PandaLogUtils.getInstance().i("键盘关闭");                    //重新算价                    calculate();                }            };

更多相关文章

  1. Android完美禁用Home键
  2. android简单的动态控制软键盘的显隐
  3. android短信窃听器,娱乐版,效果可以实现
  4. android > 手机MIC,听筒音量监听
  5. Android代码实现飞行模式的打开
  6. Android(安卓)监听各个Acitivity的生命周期
  7. [置顶] android软键盘的隐藏问题
  8. Android——布局的一些动态设置
  9. android 打开ap热点

随机推荐

  1. 本博博文预报
  2. 如何去写 Android(安卓)init.rc (Android
  3. 开启 Android 反射调用
  4. Android MediaController
  5. 通过JS或PHP检测Android
  6. Android在设置里面添加新功能的方法
  7. 关于android:focusable属性
  8. [AndroidTips]Android预定义样式
  9. 基于百度地图API的Android公交换乘导航
  10. Android API Level对应Android版本一览表