如何使滑动抽屉远离虚拟键盘顶部

发布于 2024-10-09 01:02:26 字数 323 浏览 5 评论 0原文

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

有什么想法吗?

I've been scratching my head all day over this. on one of my activities (and only one) when i call up the virtual keyboard the sliding drawer handle appears above it. i managed to fix this problem on all the other activities in my app by putting android:windowSoftInputMode="adjustPan" in every activity in my Manafest.xml file including the activity in question. also as far as i've been able to determine none of the objects on the activity has focus (if one does i cant figure out how to find it). i've checked for focus by using this.getCurrentFocus() then doing view.clearFocus() on the returned view if there was one. so far it hasn't returned a view so as far as i can tell nothing has focus.

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2024-10-16 01:02:26

这是我的解决方法。在保存 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()MyActivityonCreate()

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

This is my workaround.. in the Activity that holds the EditText I have a subclass that extends EditText. In this subclass I override the onMeasure() method which checks to see if the keyboard is open.

    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);
            }
    }

Then in the activity, if the keyboard is open the drawer handle is set to a 1px x 1px transparent image, if the keyboard is hidden the real handle is shown:

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);
}

Finally, make sure you call setContext() in onCreate() of MyActivity

MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文