关于找到显示/隐藏软键盘事件的文章有很多。我现在遇到了这样一种情况:我需要在一个片段中根据软键盘状态更改图标。
我尝试使用onMeasure进行实现,但是我不能在我的片段中重写它。是否有一种(相对)简单的方法在我的片段中获得干净的显示/隐藏软键盘事件,或者我应该放弃?
关于找到显示/隐藏软键盘事件的文章有很多。我现在遇到了这样一种情况:我需要在一个片段中根据软键盘状态更改图标。
我尝试使用onMeasure进行实现,但是我不能在我的片段中重写它。是否有一种(相对)简单的方法在我的片段中获得干净的显示/隐藏软键盘事件,或者我应该放弃?
可悲但事实 - 安卓没有本地的软件键盘显示事件。
处理键盘隐藏的方法之一是检查输入的符号和返回按钮按下(例如,textEdit将接收甚至是返回按钮)- 但这不是足够灵活的解决方案。
另一个可能的解决方案是:在activity中覆盖onMeasure,然后通知观察者(观察者模式)- 例如fragments。Fragment应该在onPause onResume事件中订阅和取消订阅自己。类似于以下activity代码:
private class DialogActivityLayout extends LinearLayout {
public DialogActivityLayout(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.activity_dialog, this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
/* Layout loaded */
if (actualHeight == 0 || proposedHeight == actualHeight) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
if (proposedHeight > actualHeight) {
DialogActivity.this.onKeyboardHide();
} else {
DialogActivity.this.onKeyboardShow();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
我不确定,但据我记得它只适用于LinearLayout,当然activity应该设置adjustResize
标志(以编程方式或在清单中)
另一种(我认为更好的方法)是使用globalTree观察者来实现
{{link1:Android中活动中软键盘打开和关闭监听器?}}
经过一番努力,我成功地抽象出了一个方法,每当我需要显示或隐藏键盘时,我都可以调用它,而不使用我在许多答案中看到的SHOW_FORCED,其中结果是即使在没有文本输入的新活动中也会打开键盘。
我在onGlobalLayout中使用了这个代码来检查键盘是否打开,然后在我的方法中决定是否要打开或关闭。
以下是代码:
检查它是否打开:
private static boolean isKeyboardVisible(Activity activity) {
Rect r = new Rect();
View contentView = activity.findViewById(android.R.id.content);
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
return (keypadHeight > screenHeight * 0.15);
}
为了执行我需要的动作(这里是我调用上面的方法的地方):
public static void toggleKeyboard(final Activity activity, final boolean showKeyboard) {
final InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
// The Handler is needed because the method that checks if the keyboard
// is open need some time to get the updated value from the activity,
// e.g. when my activity return to foreground.
new Handler().postDelayed(new Runnable() {
public void run() {
// This 'if' just check if my app still in foreground
// when the code is executed to avoid any problem.
// I've leave out of the answer to keep short, you may use your own.
if(Tools.isAppInForeground(activity)) {
// Check the keyboard.
boolean isVisible = isKeyboardVisible(activity);
// If I want to show the keyboard and it's not visible, show it!
if (showKeyboard && !isVisible) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_IMPLICIT_ONLY);
// If I want to hide and the keyboard is visible, hide it!
} else if (!showKeyboard && isVisible) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}
}, 100);
}
使用时,我只需要这样调用:
toggleKeyboard(myactivity, true); // show
// or
toggleKeyboard(myactivity, false); // hide