第一个计划:
自定义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,博客非常清楚,谢谢博客作者

具体用法:
软键盘收回显示器:
//键盘隐藏 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名称;活动:上下文
第三个选项:
自定义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; } } }); }
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-374174-1.html
只能使战争早点到来
马上赶过来送上几部意思意思
又在这里自娱自乐
有3000万人