Android软键盘弹出和收起的监听

这篇博客介绍了如何在Android应用中实现软键盘的监听和控制。通过SoftKeyboardHelper工具类,可以轻松监听软键盘的弹出和隐藏,并提供回调方法进行相应操作。在布局变更时检测屏幕高度变化,从而判断软键盘的状态。这对于需要调整布局以适应软键盘显示的应用场景非常有用。
摘要由CSDN通过智能技术生成

Android软键盘弹出和收起的监听

1.直接调用

SoftKeyboardHelper softKeyboardHelper = softKeyboardHelper = new SoftKeyboardHelper(this.context);
softKeyboardHelper.setKeyboardListener(rootView, new SoftKeyboardHelper.SoftKeyboardListener() {
            @Override
            public void onSoftKeyboardShow(int softKeyboardHeight) {
                ToastUtil.showToast("键盘弹出");
                }
            }

            @Override
            public void onSoftKeyboardHide(int softKeyboardHeight) {
                ToastUtil.showToast("键盘收起");
                }
            }
        });
  1. SoftKeyboardHelper.class
/**
 * @Description 软键盘工具类
 */
public class SoftKeyboardHelper {
    public static final String TAG = "SoftKeyboard_Debug";
    private InputMethodManager imm;
    private final Context context;
    private int lastScreenOrientation;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public SoftKeyboardHelper(Context context) {
        this.context = context;
        if (ScreenUtils.isPortrait()) {
            lastScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
        } else {
            lastScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }


    /**
     * 显示软键盘
     *
     * @param view 触发软键盘的EditText
     */
    public synchronized void showSoftKeyboard(EditText view) {
        if (imm == null) {
            imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        imm.showSoftInput(view, 0);
    }

    /**
     * 隐藏软键盘
     *
     * @param view 触发软键盘的EditText
     */
    public synchronized void hideSoftKeyboard(EditText view) {
        if (imm == null) {
            imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        if (view != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    /**
     * 监听软键盘弹出监听
     *
     * @param parentLayout         父容器View
     * @param softKeyboardListener 监听接口
     */
    public void setKeyboardListener(final View parentLayout, final SoftKeyboardListener softKeyboardListener) {
        if (softKeyboardListener == null || parentLayout == null) {
            return;
        }
        parentLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int screenHeight = ScreenUtils.getScreenHeight();
                int currentScreenOrientation = -1;
                if (ScreenUtils.isPortrait()) {
                    currentScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
                } else {
                    currentScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
                }
                //排除横竖屏切换引起的布局变化
                if (lastScreenOrientation != currentScreenOrientation) {
                    lastScreenOrientation = currentScreenOrientation;
                    return;
                }

                int defaultHeight = screenHeight / 3;
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > defaultHeight)) {
                    softKeyboardListener.onSoftKeyboardShow(0);
                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > defaultHeight)) {
                    softKeyboardListener.onSoftKeyboardHide(0);
                }

            }
        });
    }


    /**
     * 软键盘事件监听接口
     */
    public interface SoftKeyboardListener {
        /**
         * 软键盘弹出监听回调
         *
         * @param softKeyboardHeight 软键盘高度
         */
        void onSoftKeyboardShow(int softKeyboardHeight);

        /**
         * 软键盘隐藏监听回调
         *
         * @param softKeyboardHeight 软键盘高度
         */
        void onSoftKeyboardHide(int softKeyboardHeight);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值