如何将滑动抽屉从虚拟键盘顶部移开

iHo*_*rse 5 keyboard android focus drawer

我一整天都在摸不着头脑.在我的一个活动(并且只有一个)上,当我调用虚拟键盘时,滑动抽屉把手出现在它上面.我设法通过在我的Manafest.xml文件中的每个活动中放置android:windowSoftInputMode ="adjustPan"来解决我的应用程序中所有其他活动的问题,包括相关活动.另外,我已经能够确定活动中没有任何对象具有焦点(如果我不知道如何找到它).我已经通过使用this.getCurrentFocus()检查了焦点,然后在返回的视图上执行view.clearFocus()(如果有的话).到目前为止它还没有返回一个视图,所以我可以说什么都没有焦点.

有任何想法吗?

Mat*_*t M 0

这是我的解决方法。在保存 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);
            }
    }
Run Code Online (Sandbox Code Playgroud)

然后在活动中,如果键盘打开,抽屉手柄设置为 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);
}
Run Code Online (Sandbox Code Playgroud)

最后,请确保您setContext()致电onCreate()MyActivity

MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);
Run Code Online (Sandbox Code Playgroud)