Android: How do I prevent the soft keyboard from pushing my view up?

后端 未结 28 1324
南笙
南笙 2020-11-22 05:10

I have a vertical sliding drawer at the bottom of my app. When the soft keyboard opens, it pushes the tab for the drawer up, so it sits atop the keyboard. I actually want it

28条回答
  •  失恋的感觉
    2020-11-22 06:04

    For future readers.

    I wanted specific control over this issue, so this is what I did:

    From a fragment or activity, hide your other views (that aren't needed while the keyboard is up), then restore them to solve this problem:

                rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        rootView.getWindowVisibleDisplayFrame(r);
                        int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);
    
                        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                        //ok now we know the keyboard is up...
                            view_one.setVisibility(View.GONE);
                            view_two.setVisibility(View.GONE);
    
                        }else{
                        //ok now we know the keyboard is down...
                            view_one.setVisibility(View.VISIBLE);
                            view_two.setVisibility(View.VISIBLE);
    
                        }
                    }
                });
    

提交回复
热议问题