软键盘弹出和收回的三种方式:
第一种方案:
自定义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); } });
第二种方案:
软键盘弹出监听(通过高度计算,但是这种方式有问题,不推荐用):
//键盘显示监听
customized_edit.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
//当键盘弹出隐藏的时候会 调用此方法。
@Override
public void onGlobalLayout() {
final Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
final int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight();
Log.e("TAG",rect.bottom+"#"+screenHeight);
final int heightDifference = screenHeight - rect.bottom;
boolean visible = heightDifference > screenHeight / 3;
if(visible){
SysoutUtils.out("软键盘显示");
customized_submit.setVisibility(View.GONE);
}else {
SysoutUtils.out("软键盘隐藏");
customized_submit.setVisibility(View.VISIBLE);
}
}
});
注: xxxEditText:实例化的Edittext名称; activity:上下文
第三种方案:
自定义RelativeLayout
import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.RelativeLayout; public class KeyboardLayout extends RelativeLayout { private static final String TAG = KeyboardLayout.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; private boolean mHasKeybord; private int mHeight; private onKeyboaddsChangeListener mListener; public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public KeyboardLayout(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboardLayout(Context context) { super(context); } /** * set keyboard state listener */ public void setOnkbdStateListener(onKeyboaddsChangeListener listener){ mListener = listener; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (!mHasInit) { mHasInit = true; mHeight = b; if (mListener != null) { mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT); } } else { mHeight = mHeight < b ? b : mHeight; } if (mHasInit && mHeight > b) { mHasKeybord = true; if (mListener != null) { mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW); } Log.w(TAG, "show keyboard......."); } if (mHasInit && mHasKeybord && mHeight == b) { mHasKeybord = false; if (mListener != null) { mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE); } Log.w(TAG, "hide keyboard......."); } } public interface onKeyboaddsChangeListener{ public void onKeyBoardStateChange(int state); } }具体用法:
//监听软件盘是否弹起 private void onKeyBoardListener(){ xxxEdittext.setOnkbdStateListener(new KeyboardLayout.onKeyboaddsChangeListener() { public void onKeyBoardStateChange(int state) { switch (state) { case KeyboardLayout.KEYBOARD_STATE_HIDE: // UtilsToast.showToast("软键盘隐藏"); customized_submit.setVisibility(View.VISIBLE); break; case KeyboardLayout.KEYBOARD_STATE_SHOW: UtilsToast.showToast("软键盘弹起"); customized_submit.setVisibility(View.INVISIBLE); break; } } }); }