android 后台键盘按键监听,android键盘的监听

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();

}

};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值