0
@Override
public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);


   // Checks whether a hardware keyboard is available
   if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
       Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
   } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
       Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
   }
}

我想在运行时检测键盘可见性。上面的代码在 android 2.2 中不起作用。我需要一个键盘事件监听器的解决方案。

我还在 manifest.xml 中添加了 configChanges 属性

4

1 回答 1

0

这是安卓的痛点。您需要使用自定义视图作为根视图并应用使用 onSizeChanged() + 布局更改差异的侦听器来调用自定义回调。

示例视图。

/**
 * Subclass of RelativeLayout that adds a size changed listener.  This is useful for determining
 * when the onscreen keyboard has popped up and resized the window
 *
 */
public class SCRelativeLayout extends RelativeLayout {
  public interface OnSizeChangedListener {
    public void onSizeChanged(int w, int h, int oldw, int oldh);
  }

  private OnSizeChangedListener mOnSizeChangedListener;

  public SCRelativeLayout(Context context) {
    super(context);
  }

  public SCRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public SCRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    if (mOnSizeChangedListener != null) {
      mOnSizeChangedListener.onSizeChanged(w, h, oldw, oldh);
    }
  }

  public void setOnSizeChangedListener(OnSizeChangedListener listener) {
    mOnSizeChangedListener = listener;
  }
}
于 2012-09-08T08:14:34.380 回答