据我所知,你不能。
但是,您可以监视布局的大小更改,并且由于键盘显示是调整大小的主要原因,因此您可能可以假定显示或不显示键盘。
以下是用于监视布局大小更改的示例代码。只需使用此布局作为原始布局的父级,然后使用其侦听器。如果高度已经下降,则可以假定显示键盘,并且如果增加了,则可以假定它已关闭。
public class LayoutSizeChangedSensorFrameLayout extends FrameLayout {
public enum SizeChange {
HEIGHT_INCREASED, HEIGHT_DECREASED, WIDTH_INCREASED, WIDTH_DECREASED
}
public interface OnLayoutSizeChangedListener {
void onSizeChanged(EnumSet<SizeChange> direction);
}
private OnLayoutSizeChangedListener mLayoutSizeChangeListener;
public LayoutSizeChangedSensorFrameLayout(final Context context) {
super(context);
}
public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attributeSet) {
super(context, attributeSet);
}
public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mLayoutSizeChangeListener != null) {
final EnumSet<SizeChange> result = EnumSet.noneOf(SizeChange.class);
if (oldh > h)
result.add(SizeChange.HEIGHT_DECREASED);
else if (oldh < h)
result.add(SizeChange.HEIGHT_INCREASED);
if (oldw > w)
result.add(SizeChange.WIDTH_DECREASED);
else if (oldw < w)
result.add(SizeChange.WIDTH_INCREASED);
if (!result.isEmpty())
mLayoutSizeChangeListener.onSizeChanged(result);
}
}
public void setOnLayoutSizeChangedListener(final OnLayoutSizeChangedListener layoutSizeChangeListener) {
this.mLayoutSizeChangeListener = layoutSizeChangeListener;
}
public OnLayoutSizeChangedListener getOnLayoutSizeChangeListener() {
return mLayoutSizeChangeListener;
}
}
检查了这一点:http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device/31090451#31090451 适合我。 – 2015-06-27 15:41:54
[如何检查Android中软件键盘的可见性?](http:// stackoverflow。com/questions/2150078/how-to-check-android-visibility-of-software-keyboard-in-android) – afathman 2016-02-08 17:02:12