軟鍵盤彈出和收回的三種方式
第一種
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);
}
/**
*鍵盤監(jiān)聽接口
*/
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主籍,感謝該博客博主
用法
軟鍵盤收回監(jiān)聽
//鍵盤隱藏監(jiān)聽
customized_edit.setOnKeyBoardHideListener(new TextEditTextView.OnKeyBoardHideListener(){
@Override
public void onKeyHide(int keyCode, KeyEvent event) {
SysoutUtils.out("軟鍵盤隱藏");
customized_submit.setVisibility(View.VISIBLE);
}
});
第二種
軟鍵盤彈出監(jiān)聽(通過高度計(jì)算肴沫,但是這種方式有問題略贮,不推薦用):
//鍵盤顯示監(jiān)聽
customized_edit.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
//當(dāng)鍵盤彈出隱藏的時(shí)候會(huì) 調(diào)用此方法。
@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);
}
}
});
第三種
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);
}
}
用法
//監(jiān)聽軟件盤是否彈起
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;
}
}
});
}