@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属性
答案 0 :(得分:0)
这是Android的痛点。您将要使用自定义视图作为根视图,并应用使用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;
}
}