这是我的解决方法.. 在包含 EditText 的 Activity 中,我有一个扩展的子类EditText
。在这个子类中,我重写了onMeasure()
检查键盘是否打开的方法。
public static class MyEditText extends EditText {
MyActivity context;
public void setContext(MyActivity context) {
this.context = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity)getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
// assume all soft keyboards are at least 128 pixels high
if (diff>128)
context.showHandle(false);
else
context.showHandle(true);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后在活动中,如果键盘打开,抽屉手柄设置为 1px x 1px 透明图像,如果键盘隐藏,则显示真实手柄:
private void showHandle(boolean show) {
ImageView drawer_handle = (ImageView) findViewById(R.drawable.handle);
if (show)
drawer_handle.setImageResource(R.drawable.handle);
else
drawer_handle.setImageResource(R.drawable.handle_blank);
}
setContext()
最后,onCreate()
请确保您致电MyActivity
MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);