githup中找到:https://github.com/yescpu/KeyboardChangeListener

import android.app.Activity;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver; /**
* simple and powerful Keyboard show/hidden listener,view {@android.R.id.content} and {@ViewTreeObserver.OnGlobalLayoutListener}
* Created by yes.cpu@gmail.com 2016/7/13.
*/
public class KeyboardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
private static final String TAG = "ListenerHandler";
private View mContentView;
private int mOriginHeight;
private int mPreHeight;
private KeyBoardListener mKeyBoardListen; public interface KeyBoardListener {
/**
* call back
* @param isShow true is show else hidden
* @param keyboardHeight keyboard height
*/
void onKeyboardChange(boolean isShow, int keyboardHeight);
} public void setKeyBoardListener(KeyBoardListener keyBoardListen) {
this.mKeyBoardListen = keyBoardListen;
} public KeyboardChangeListener(Activity contextObj) {
if (contextObj == null) {
Log.i(TAG, "contextObj is null");
return;
}
mContentView = findContentView(contextObj);
if (mContentView != null) {
addContentTreeObserver();
}
} private View findContentView(Activity contextObj) {
return contextObj.findViewById(android.R.id.content);
} private void addContentTreeObserver() {
mContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
} @Override
public void onGlobalLayout() {
int currHeight = mContentView.getHeight();
if (currHeight == 0) {
Log.i(TAG, "currHeight is 0");
return;
}
boolean hasChange = false;
if (mPreHeight == 0) {
mPreHeight = currHeight;
mOriginHeight = currHeight;
} else {
if (mPreHeight != currHeight) {
hasChange = true;
mPreHeight = currHeight;
} else {
hasChange = false;
}
}
if (hasChange) {
boolean isShow;
int keyboardHeight = 0;
if (mOriginHeight == currHeight) {
//hidden
isShow = false;
} else {
//show
keyboardHeight = mOriginHeight - currHeight;
isShow = true;
} if (mKeyBoardListen != null) {
mKeyBoardListen.onKeyboardChange(isShow, keyboardHeight);
}
}
} public void destroy() {
if (mContentView != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mContentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
}

KeyboardChangeListener

simple and powerful Keyboard show/hidden change listener,without having to add a layout and can run in every Activity;

useage

make activity:

android:windowSoftInputMode="adjustResize"

java:

new KeyboardChangeListener(this).setKeyBoardListener(new KeyboardChangeListener.KeyBoardListener() {
@Override
public void onKeyboardChange(boolean isShow, int keyboardHeight) {
Log.d(TAG, "isShow = [" + isShow + "], keyboardHeight = [" + keyboardHeight + "]");
}
});

#enjoy!

感谢作者!

最新文章

  1. Linux下用户组、文件权限详解
  2. 【cl】Json学习
  3. TCP/IP WebSocket MQTT
  4. JDK,JRE,JVM区别与联系-理解与概括
  5. C#_事件委托
  6. [置顶] mybatis批量新增系列之有主键的表的批量新增
  7. 【java提高】Serializable(一)--初步理解
  8. Windows DLL资料整理
  9. WebService访问oracle数据库本地调试
  10. YAML配置:mapping values are not allowed here
  11. html-form
  12. Linux使用命令修改默认启动为图形或字符界面
  13. Conda下安装libsvm
  14. i=i+1,i+=1,i++哪个执行效率最高?为什么?
  15. SpringBoot 开启 Actuator
  16. C#(简单递归)和实现IComparable接口
  17. Restore IP Addresses,将字符串转换成ip地址
  18. HDU 2553 N皇后问题 (深搜)
  19. JavaFx 实现画图工具
  20. 牛客网 牛客小白月赛1 E.圆与三角形-公式题

热门文章

  1. 线程流量控制工具之Semaphore
  2. linux PHP 安装及 GD库安装
  3. 无法启动此程序,因为计算机中丢失 MSVCP120.dll。尝试安装该程序以解决此问题
  4. 用DVD镜像离线安装Debian的软件包
  5. DotnetBrowser高级教程-(4)使用MVC框架3-文件上传
  6. ES6里关于类的拓展(二):继承与派生类
  7. 【Hive】Hive 安装&使用基础
  8. 使用老版本的java api提交hadoop作业
  9. 2017.7.18 linux下ELK环境搭建
  10. Effective C++ 条款17