yescpu / keyboardchangelistener Goto Github PK
View Code? Open in Web Editor NEWSimple and powerful keyboard show/hidden listeners
Simple and powerful keyboard show/hidden listeners
我设置完这个属性之后:android:windowSoftInputMode="adjustResize"
每次一进页面键盘就会自动弹起,而我不想要这个效果(;´д`)ゞ
部分机型没用
嗯,我刚开始也觉得没用,通过打印LOG发现监听屏幕高度并没有变化。。。
就很吃🐳。
仔细想想可能是自己使用方法不对。
于是一个个试,终于 发现是自己 代码设置问题。
由于考虑到 不同机型 不同厂家的皮。
请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!
请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!
请大家 先把自己 有关所有 状态栏设置代码屏蔽后 尝试使用该库!!!
应该是 与 FULL_SCEEN_MODEL 等设置有关,具体的没时间去尝试。
如果还是没有,那就是运气不好了。
尝试其他办法吧,另外拿来主义也不要这么🐻呀,大家心平气!@¥#¥#@%@¥%@%@#%¥……#¥%#¥
hi, yescpu, To use adjustNothing and to listen to the keyboard, what do you need to do with WindowManager? Is there a link that can be attached? Thank you.
不行
作者的是有点问题,更改为这个就可以了:
public class CqrKeyboardListen implements ViewTreeObserver.OnGlobalLayoutListener {
private View contentView;
private int originVisibleH;
private OnKeyboardListener onKeyboardListener;
private CqrKeyboardListen(Activity activity, OnKeyboardListener listener) {
if (activity == null) {
return;
}
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
onKeyboardListener = listener;
contentView = activity.findViewById(android.R.id.content);
if (contentView != null) {
contentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
}
public static CqrKeyboardListen listen(Activity activity, OnKeyboardListener listener) {
return new CqrKeyboardListen(activity, listener);
}
@Override
public void onGlobalLayout() {
if (onKeyboardListener == null) {
return;
}
//布局变化,获取当前根视图在屏幕上显示的高度
Rect rect = new Rect();
contentView.getWindowVisibleDisplayFrame(rect);
int currentVisibleH = rect.height();
if (originVisibleH == 0) {
originVisibleH = currentVisibleH;
}
int diffHeight = originVisibleH - currentVisibleH;
//CqrLogPrinter.printLog("originVisibleH=" + originVisibleH + ",currentVisibleH="
// + currentVisibleH + ",diffHeight=" + diffHeight);
if (diffHeight > 200) {
//根视图显示高度变小超过200,可以看作软键盘显示了
onKeyboardListener.onKeyboardChange(true, diffHeight);
originVisibleH = currentVisibleH;
return;
}
if (diffHeight < -200) {
//根视图显示高度变大超过200,可以看作软键盘隐藏了
onKeyboardListener.onKeyboardChange(false, diffHeight);
originVisibleH = currentVisibleH;
}
}
/**
* 在{@link Activity#onDestroy()}中调用,防止内存泄漏
*/
public void destroy() {
if (contentView != null) {
CqrCompatUtils.removeOnGlobalLayoutListener(contentView, this);
}
}
public interface OnKeyboardListener {
void onKeyboardChange(boolean isShow, int keyboardHeight);
}
}
使用:
keyboardListen = CqrKeyboardListen.listen(activity, new CqrKeyboardListen.OnKeyboardListener() {
@Override
public void onKeyboardChange(boolean isShow, int keyboardHeight) {
// DO SOMETHING
}
});
@Override
protected void onDestroy() {
super.onDestroy();
keyboardListen.destroy();
}
这里的回调仅仅当 mShowFlag != currentShow
条件成立的时候才会回调,这里其实是有问题的。
EditText
,而且inputType
的类型不相同的时候,会出现问题,具体操作如下:
EditText
,一个 EditText
的inputType
为phone
,另外一个inputType
为textPassword
.EditText
,弹出只能输入数字的键盘,他的高度在我的手机上是691 像素EditText
, 会弹出可以同时输入账号和密码的键盘,高度为912像素为了方便测试,我自己在你的基础上添加了额外的测试信息
override fun onGlobalLayout() {
...
Log.d(TAG, "onGlobalLayout() called screenHeight $screenHeight VisibleDisplayHeight $windowBottom, keyboardHeight: $keyboardHeight")
val currentShow = keyboardHeight > MIN_KEYBOARD_HEIGHT
Log.d(TAG, "mShowFlag: $mShowFlag, currentSHow: $currentShow")
if (mShowFlag != currentShow) {
mShowFlag = currentShow
keyboardListener.onKeyboardChange(currentShow, keyboardHeight)
}
}
onKeyboardChange
方法,因为mShowFlag != currentShow
// 这个是第一次点击输入手机号的时候
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
D/KeyboardChangeListener: mShowFlag: false, currentShow: true
// 这个是第二次点击输入密码 EditText的时候,正常情况,每切换一次,会出现 3 对日志(共6条)
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1920, keyboardHeight: 0
D/KeyboardChangeListener: mShowFlag: true, currentShow: false
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1008, keyboardHeight: 912
D/KeyboardChangeListener: mShowFlag: false, currentShow: true
onKeyboardChange
方法。
mShowFlag = true, currentShow = true
,但是键盘的高度是不同的,从这里也可以得出解决方法。// 异常情况,只会出现 2 对日志(共 4)条日志
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1008, keyboardHeight: 912
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
D/KeyboardChangeListener: onGlobalLayout() called screenHeight 1920 VisibleDisplayHeight 1229, keyboardHeight: 691
D/KeyboardChangeListener: mShowFlag: true, currentShow: true
if (mShowFlag != currentShow || (mShowFlag && (previousKeyboardHeight != keyboardHeight))) {
mShowFlag = currentShow
keyboardListener.onKeyboardChange(currentShow, keyboardHeight)
}
rt
并没有什么乱用
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.