【发布时间】:2017-01-18 18:44:18
【问题描述】:
我想检测软键盘何时显示和隐藏。为此,我喜欢这样:
public class ALEditText extends EditText {
private ALSoftInputListener mSoftInputListener;
private static class SoftInputReceiver extends ResultReceiver {
private static final int RESULT_UNCHANGED_HIDDEN = 1;
private static final int RESULT_SHOWN = 2;
private static final int RESULT_HIDDEN = 3;
private static final int RESULT_UNCHANGED_SHOWN = 0;
private ALSoftInputListener mListener;
public SoftInputReceiver(ALSoftInputListener listener) {
super(null);
this.mListener = listener;
}
public void onReceiveResult(int result, Bundle data) {
switch (result) {
case RESULT_UNCHANGED_SHOWN /*0*/:
case RESULT_SHOWN /*2*/:
if (this.mListener != null) {
this.mListener.onSoftInputShown();
}
case RESULT_UNCHANGED_HIDDEN /*1*/:
case RESULT_HIDDEN /*3*/:
if (this.mListener != null) {
this.mListener.onSoftInputHidden();
}
default:
}
}
}
public void showSoftInput() {
SoftInputReceiver receiver = new SoftInputReceiver(this.mSoftInputListener);
InputMethodManager imm = getInputMethodManager();
imm.showSoftInput(this, 0, receiver);
}
public void HideSoftInput() {
SoftInputReceiver receiver = new SoftInputReceiver(this.mSoftInputListener);
InputMethodManager imm = getInputMethodManager();
imm.hideSoftInputFromWindow(getWindowToken(), 0, receiver);
}
public void SetSoftInputListener(ALSoftInputListener listener) {
this.mSoftInputListener = listener;
}
protected InputMethodManager getInputMethodManager() {
return (InputMethodManager) getContext().getSystemService("input_method");
}
}
但我有一个奇怪的行为,我无法理解,当我执行 showSoftInput 时,虚拟键盘会显示,并且 onSoftInputShown 事件也会引发,但紧随其后的是 onSoftInputHidden,键盘仍然可见!稍后如果我隐藏键盘事件 onSoftInputHidden 将不会再次调用...有人可以解释我发生了什么吗?
【问题讨论】:
标签: android keyboard android-softkeyboard keyboard-events android-event