我在前段时间编写的应用程序中遇到了同样的问题。它现在已停产,但这不是你的问题 xD。
事实上,没有选项可以跟踪这样的操作,但我找到了一个很好的(或多或少)解决方案来添加这样的功能。这在理论上很简单,但我认为它也是一个“黑客”。
因此,您需要一个包含您的应用程序或特殊区域的自定义线性布局。之后,您必须为其添加一个侦听器。这仅适用于纵向模式。
所以这里是代码:(对不起,我不记得出处了)
自定义布局:
LinearLayoutThatDetactsSoftwarekeyboard.java(这是布局 xD 的原始名称)
package com.tundem.people.Layout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/*
* LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when
* the soft keyboard is shown and hidden (something Android can't tell you, weirdly).
*/
public class LinearLayoutThatDetectSoftkeyboard extends LinearLayout {
public LinearLayoutThatDetectSoftkeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity) getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
if (listener != null) {
listener.onSoftKeyboardShown(diff > 128); // assume all soft
// keyboards are at
// least 128 pixels high
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
以及如何添加监听器:
final LinearLayoutThatDetectSoftkeyboard lltodetectsoftkeyboard = (LinearLayoutThatDetectSoftkeyboard) findViewById(R.id.LinearLayout_SoftKeyboard);
lltodetectsoftkeyboard.setListener(new Listener() {
public void onSoftKeyboardShown(boolean isShowing) {
if (actMode == MODE_SMS && isShowing) {
findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.GONE);
} else {
findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.VISIBLE);
}
}
});
LinearLayout 添加了一个监听器,每当 layoutheight 改变至少 128 像素时都会调用该监听器。这是一个技巧,它不适用于小于 128 像素的键盘(但我认为每个键盘都有这样的高度)
如果 LayoutHeight 已更改,您将收到通知是否现在显示。
希望我的回答有用。也许您再次在 StackOverFlow 上找到了真正的来源。所以我不会偷别人的天才。学分归未知人所有;)