Android 监听键盘的关闭和打开
来源:互联网 发布:淘宝宝贝裂变违规吗 编辑:程序博客网 时间:2024/05/10 13:00
自定义的类import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver;import java.util.LinkedList;import java.util.List;/** * sjw * 2016/5/18 * 13:31 */public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); //r will be populated with the coordinates of your view that area still visible. activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** * Default value is zero (0) * @return last saved keyboard height in px */ 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(); } } }}在监听的地方final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(view);//这里的view是整个界面的ViewsoftKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() { @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { //打开键盘 } @Override public void onSoftKeyboardClosed() { //隐藏键盘 } });
0 0
- Android 监听键盘的关闭和打开
- Android 监听手机键盘打开和关闭状态
- Android 打开关闭键盘以及监听的工具类
- 安卓监听键盘的打开和关闭
- Android监听键盘开启关闭的方法
- Android 软键盘的打开关闭监听
- 关闭和打开键盘的通知
- 关闭和打开键盘的通知
- 关闭和打开键盘的通知
- Android 监听键盘弹出关闭
- Android 监听键盘的弹出与关闭,收起
- Android监听键盘的显示和隐藏
- android键盘的回车键修改和监听
- android 监听键盘的弹起和隐藏
- 键盘的关闭打开通知
- 键盘的打开与关闭
- [Android] 监听系统网络连接打开或者关闭的消息
- Android 设置Wifi热点、打开与关闭的监听
- 怎么区分域名级别,举例说明更易懂
- Android 四大组件——活动 巧用 Activity 和 Fragment
- hive的使用
- json和js字面量对象对比以及json序列号和反序列化的技术细节
- make.centerY.equalTo(weakSelf).with.offset(-110);
- Android 监听键盘的关闭和打开
- StringBuffer的简单理解
- 【git】常用命令
- Android studio 和Git工具使用
- 一颗成熟的 树
- iOS 数据本地存取
- C++中string转为int
- JavaScript基本语法
- Android的蓝牙串口(Bluetooth SPP)使用