简体   繁体   English

键盘打开时上移弹出 window

[英]Move up Popup window when keyboard opens

I have an activity window (Activity pic), when I click the second button, popup window appears at the bottom (it's good, I need that) of screen (PopUp pic), when i click on the edit text field, brings up the keyboard, but it cover the popup window edit text fields (Keyboard 3).我有一个活动 window(活动图片),当我单击第二个按钮时,弹出窗口 window 出现在屏幕(弹出图片)的底部(很好,我需要它),当我单击编辑文本字段时,弹出键盘,但它覆盖弹出窗口 window 编辑文本字段(键盘 3)。 Where is my error that the popup window does not up when the keyboard appears?当键盘出现时弹出窗口 window 没有弹出,我的错误在哪里? Do you have any ideas?你有什么想法?

Screen shoots屏幕截图

Activity活动

PopUp弹出

Keyboard键盘

Pop-Up Window class弹窗 Window class

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width),(int)(height*.4));

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
       
        getWindow().setAttributes(params);
PopupWindow popup = new PopupWindow(
         popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);

Then the only thing you need is a Listener for the Keyboard (or more general: For Window Height changes).然后你唯一需要的是键盘的监听器(或更一般的:对于 Window 高度变化)。 This was actually easier than I thought - and it didn't require any special access like an Activity-object or similar.这实际上比我想象的要容易——而且它不需要像 Activity 对象或类似对象那样的任何特殊访问权限。 Even in my independent View-class which only knows the Context (which I didn't want to cast), I was able to accomplish that.即使在我只知道上下文(我不想投射)的独立视图类中,我也能够做到这一点。 Everything you need is only one View-object which has already been added to the layout.您需要的只是一个已经添加到布局中的视图对象。

final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        Rect r = new Rect();
        root.getWindowVisibleDisplayFrame(r);

        // Calculate the difference between the original height and the new height
        int heightDiff = r.height() - root.getHeight();

        // Now update the Popup's position
        // The first value is the x-axis, which stays the same.
        // Second value is the y-axis. We still want it centered, so move it up by 50% of the height
        // change
        // The thir`enter code here`d and the fourth values are default values to keep the width/height
        popup.update(0, heightDiff / 2, -1, -1);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM