有时候需要判断该软键盘是否弹出:
方法1:
通过InputMethodManager类来判断,但是isActive()判断并不准确,有的时候你的键盘已经关闭了,但是依然返回ture。
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //得到InputMethodManager的实例
if (imm.isActive()) {//如果开启
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_NOT_ALWAYS
);//关闭/开启软键盘
}
方法2:
通过判断布局中的RelativeLayout的高度,Activity设置:
android:windowSoftInputMode="adjustResize"
然后键盘弹出的时候RelativeLayout会被挤压,从而高度发生变化,由此判断软键盘的状态。
为了方便使用,我们自定义一个RelativeLayout:
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
/** * 监听软键盘是否处于打开状态,使用方式RelativeLayout一样 */
public class KeyboardListenRelativeLayout extends RelativeLayout {
private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();
public static final byte KEYBOARD_STATE_SHOW = -3;
public static final byte KEYBOARD_STATE_HIDE = -2;
public static final byte KEYBOARD_STATE_INIT = -1;
private boolean mHasInit = false;
private boolean mHasKeyboard = false;
private int mHeight;
private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
public KeyboardListenRelativeLayout(Context context) {
super(context);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Log.e(TAG, "b= " + b);
if (!mHasInit) {
mHasInit = true;
mHeight = b;
Log.e(TAG, "mHeight= " + b);
if (onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) { //mHeight代表键盘的真实高度 ,b代表在窗口中的高度 mHeight>b 说明键盘隐藏
mHasKeyboard = true;
if (onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
}
}
if (mHasInit && mHasKeyboard && mHeight == b) { // mHeight = b 说明已经弹出
mHasKeyboard = false;
if (onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
}
}
}
public interface IOnKeyboardStateChangedListener {
void onKeyboardStateChanged(int state);
}
}
最后给mRelativeLayout设置一个监听:
mRelativeLayout.setOnKeyboardStateChangedListener(new KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener() {
public void onKeyboardStateChanged(int state) {
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏
LogUtil.e("Keyboard", "软键盘隐藏");
break;
case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示
LogUtil.e("Keyboard", "软键盘显示");
break;
default:
break;
}
}
});
参考不详…