Theo như tôi biết, bạn không thể.
Tuy nhiên, bạn có thể theo dõi thay đổi kích thước bố cục và vì bàn phím hiển thị là nguyên nhân chính để thay đổi kích thước, bạn có thể giả định rằng bàn phím được hiển thị hay không.
Dưới đây là mã mẫu để theo dõi thay đổi kích thước bố cục. Chỉ cần sử dụng bố cục này làm bố cục gốc của bố cục ban đầu của bạn và sử dụng trình nghe của nó. Nếu chiều cao đã giảm, bạn có thể giả sử bàn phím được hiển thị và nếu nó được tăng lên, bạn có thể cho rằng nó đã bị đóng.
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;
}
}
Nguồn
2014-09-17 16:34:36
Kiểm tra này ra: http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device/31090451#31090451 Hoạt động tốt cho tôi. –
Bản sao có thể có của [Cách kiểm tra khả năng hiển thị của bàn phím phần mềm trong Android?] (Http: // stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android) – afathman