[置顶] 监听软键盘打开或关闭触发动作
2016-12-19 20:14
423 查看
/**
* Created by chaoren on 2016/12/13.
* 软键盘关闭,打开监听工具类,文章下面有使用方法
*/
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
public interface SoftKeyboardStateListener {
void onSoftKeyboardOpened(int keyboardHeightInPx);
}
/**
*下面是使用方法
/
final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.edittext_money));
softKeyboardStateHelper.addSoftKeyboardStateListener(this);
//回调
//打开软键盘触发的方法
@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
//关闭软键盘触发的方法
@Override
public void onSoftKeyboardClosed() {
* Created by chaoren on 2016/12/13.
* 软键盘关闭,打开监听工具类,文章下面有使用方法
*/
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; } 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(findViewById(R.id.edittext_money));
softKeyboardStateHelper.addSoftKeyboardStateListener(this);
//回调
//打开软键盘触发的方法
@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
}
//关闭软键盘触发的方法
@Override
public void onSoftKeyboardClosed() {
}
相关文章推荐
- 摘要MVP概念初识
- mongo 图形客户端解决无法添加拷贝数据库问题
- Perl知识点滴
- Linux上vi(vim)编辑器使用教程
- 将来的你,一定会感谢现在拼命的自己
- 如何使用3D MAX建造出DNA双螺旋结构
- chnsCompute和chnsPyramid
- C/S架构对服务器的影响
- 算法导论学习日记(4)插入排序的递归版本
- JZOJ 4923. 巧克力狂欢
- 20161219笔记
- Struts之国际化
- mongodb+pycharm使用报错,无法往mongodb存文件
- App 卸载记录
- android抛物线动画实现
- 小博老师解析Java核心技术 ——JSwing文本域和滚轴控件
- jmeter接口自动化测试之一:jmeter+maven+eclipse搭建
- c::动态顺序表的简单实现
- ycsb cassandra 安装测试
- HoneyDrive_3实践学习_1.0