监听软键盘是否弹出
很多情况下我们会用到监听软键盘弹出的需求(以下方法亲测有效):
https://blog.csdn.net/hello201404/article/details/81111082
https://github.com/yescpu/KeyboardChangeListener/blob/yescpu-patch-1/keyboardchangelib/src/main/java/com/yescpu/keyboardchangelib/KeyboardChangeListener.java
新建实现类
/**
* 监听软键盘是否弹出
* Created by DELL on 2018/7/19.
*/
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) {
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) {
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);
}
}
}
}
之后这么用就可以了
KeyboardChangeListener softKeyboardStateHelper = new KeyboardChangeListener(this);
softKeyboardStateHelper.setKeyBoardListener(new KeyboardChangeListener.KeyBoardListener() {
@Override
public void onKeyboardChange(boolean isShow, int keyboardHeight) {
if (isShow) {
//键盘的弹出
et_seartch.setCursorVisible(true);
} else {
//键盘的收起
et_seartch.setCursorVisible(false);
}
}l
});
https://blog.csdn.net/sinat_31311947/article/details/53914000 Android软键盘(六)如何监听到软件盘显示与隐藏的事件
Android软键盘弹出和收回监听
https://blog.csdn.net/wangkai1101/article/details/79066046
第一种方案:
自定义Edittext
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;
/**
* 拦截键盘向下的 EditTextView
*/
public class TextEditTextView extends EditText {
public TextEditTextView(Context context) {
super(context);
}
public TextEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
super.onKeyPreIme(keyCode, event);
onKeyBoardHideListener.onKeyHide(keyCode, event);
return false;
}
return super.onKeyPreIme(keyCode, event);
}
/**
*键盘监听接口
*/
OnKeyBoardHideListener onKeyBoardHideListener;
public void setOnKeyBoardHideListener(OnKeyBoardHideListener onKeyBoardHideListener) {
this.onKeyBoardHideListener = onKeyBoardHideListener;
}
public interface OnKeyBoardHideListener{
void onKeyHide(int keyCode, KeyEvent event);
}
}
为什么 重写 Activity.onKeyDown() 方法为什么没有用?而用的是 onKeyPreIme,该博客写的很明白http://blog.csdn.net/yxhuang2008/article/details/53822072,感谢该博客博主
具体用法:
软键盘收回监听:
//键盘隐藏监听
customized_edit.setOnKeyBoardHideListener(new TextEditTextView.OnKeyBoardHideListener(){
@Override
public void onKeyHide(int keyCode, KeyEvent event) {
SysoutUtils.out("软键盘隐藏");
customized_submit.setVisibility(View.VISIBLE);
}
});
---------------------
作者:王凯-peterWang
来源:CSDN
原文:https://blog.csdn.net/wangkai1101/article/details/79066046
版权声明:本文为博主原创文章,转载请附上博文链接!
Android监听键盘弹出收起
https://blog.csdn.net/u011181222/article/details/52043001
package com.ljh.softkeyboardlistener;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;
/**
* Created by liujinhua on 15/10/25.
*/
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
System.out.println(""+visibleHeight);
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}
SoftKeyBoardListener.setListener(AdjustResizeActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
Toast.makeText(AdjustResizeActivity.this, "键盘显示 高度" + height, Toast.LENGTH_SHORT).show();
}
@Override
public void keyBoardHide(int height) {
Toast.makeText(AdjustResizeActivity.this, "键盘隐藏 高度" + height, Toast.LENGTH_SHORT).show();
}
});