Pour autant que je sache, vous ne peut pas. Toutefois, vous pouvez surveiller les changements de taille de votre mise en page et, étant donné que le clavier est la principale cause des redimensionnements, vous pouvez supposer que le clavier est affiché ou non.
Voici un exemple de code pour surveiller les changements de taille d'une mise en page. Utilisez simplement cette disposition en tant que parent de votre mise en page d'origine et utilisez son écouteur. Si la hauteur a diminué, vous pouvez supposer que le clavier est affiché, et s'il a été augmenté, vous pouvez supposer qu'il s'est fermé.
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;
}
}
Check this out: http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device/31090451#31090451 Fonctionne bien pour moi. –
Copie possible de [Comment vérifier la visibilité du clavier logiciel dans Android?] (Http: // stackoverflow.com/questions/2150078/how-to-check-visibilité-du-logiciel-clavier-dans-android) – afathman