Android监听键盘状态变化

本文介绍了如何在Android中监听键盘的打开和关闭状态,提供了React Native的键盘监听代码示例,并提到了这种方法可能对应用性能产生的影响。

如下监听键盘状态变化的代码摘自React native的键盘状态监听代码,

这个方法会影响部分性能,正常情况下可以忽略。


import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Keyboard status monitor.
 *
 * usage:
 *
 * View view;//View must be had added to the View Tree
 *
 *
 * view.getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardListener(view){
 *
 *     @Override
 *     public void keyboardStatusChanged(boolean keyboardDidShow, @Nullable KeyboardInfo params){
 *          if(keyboardDidShow){
 *              //showing or height changed
 *          }else{
 *              //hidden
 *          }
 *     }
 *
 * });
 */
public abstract class KeyboardListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private int mKeyboardHeight = 0;
    private final Rect mVisibleViewArea = new Rect();
    private View view;
    private DisplayMetrics mDisplayMetrics;

    public KeyboardListener(View view) {
        if (view == null) {
            throw new NullPointerException("view is null");
        }

        mDisplayMetrics = view.getContext().getResources().getDisplayMetrics();
        this.view = view;
    }

    @Override
    public void onGlobalLayout() {
        View rootView = view.getRootView();
        if (rootView == null) {
            return;
        }
        rootView.getWindowVisibleDisplayFrame(mVisibleViewArea);
        final int heightDiff =
                mDisplayMetrics.heightPixels - mVisibleViewArea.bottom;
        if (mKeyboardHeight != heightDiff && heightDiff > 0) {
            // keyboard is now showing, or the keyboard height has changed
            mKeyboardHeight = heightDiff;

            KeyboardInfo keyboardInfo = new KeyboardInfo();
            keyboardInfo.screenX = mVisibleViewArea.left;
            keyboardInfo.screenY = mVisibleViewArea.bottom;
            keyboardInfo.width = mVisibleViewArea.width();
            keyboardInfo.height = mKeyboardHeight;

            sendEvent(true, keyboardInfo);
        } else if (mKeyboardHeight != 0 && heightDiff == 0) {
            // keyboard is now hidden
            mKeyboardHeight = heightDiff;
            sendEvent(false, null);
        }
    }

    private void sendEvent(boolean keyboardDidShow, @Nullable KeyboardInfo keyboardInfo) {
        keyboardStatusChanged(keyboardDidShow, keyboardInfo);
    }

    public abstract void keyboardStatusChanged(boolean keyboardDidShow, @Nullable KeyboardInfo params);

    public static class KeyboardInfo {
        /**
         * screen x pixle
         */
        public double screenY;
        public double screenX;
        public double width;
        public double height;
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值