【问题标题】:how to keep sliding drawer off the top of the virtual keyboard如何让滑动抽屉远离虚拟键盘的顶部
【发布时间】:2010-12-23 01:26:42
【问题描述】:

我整天都在为此挠头。在我的一项活动(并且只有一项)中,当我调用虚拟键盘时,滑动抽屉手柄出现在其上方。我通过将 android:windowSoftInputMode="adjustPan" 放入我的 Manafest.xml 文件中的每个活动(包括相关活动)中,设法在我的应用程序中的所有其他活动上解决了这个问题。此外,据我所知,活动上的所有对象都没有焦点(如果有的话,我不知道如何找到它)。我已经通过使用 this.getCurrentFocus() 检查焦点,然后在返回的视图上执行 view.clearFocus() 如果有的话。到目前为止,据我所知,它还没有返回视图。

有什么想法吗?

【问题讨论】:

  • 我遇到了同样的问题,并发现您的问题是第一个结果。如果您确实获得了可行的解决方案,请随时在下面发布。

标签: android keyboard focus drawer


【解决方案1】:

这是我的解决方法.. 在包含 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);
            }
    }

那么在activity中,如果键盘是打开的,抽屉手柄设置为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);
}

最后,请确保您在onCreate() of MyActivity 中调用setContext()

MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多