android Popupwindow is blocked by a pop-up soft keyboard

Keywords: Android Java xml encoding

Previously, a page contained PopupWindow, PopupWindow had an EditText input box, click EditText input PopupWindow popped out from the bottom of the screen, and the soft keyboard directly blocked PopupWindow.

When entering content, we hope the soft keyboard will not block the PopupWindow and will not affect the user's normal input. Now let's talk about how to add the following two lines of code when setting up the PopupWindow

popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

The order of these two lines of code can not be changed, otherwise it will not solve the problem, is it very different, the result of the modification is as follows, is it found that the soft keyboard pushes the PopupWindow up completely, without affecting the normal input of the user

The above example picture is a screenshot of my own actual test. Now all the code is pasted out. Students who want to see the specific effect can test it themselves.

[java] view plain copy
import android.app.Activity;  
import android.graphics.Color;  
import android.graphics.drawable.ColorDrawable;  
import android.os.Bundle;  
import android.view.Gravity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup.LayoutParams;  
import android.view.WindowManager;  
import android.widget.Button;  
import android.widget.PopupWindow;  

import com.example.mydemo.R;  

public class TestPopWindow extends Activity {  

    private PopupWindow popview;  
    private Button btn_open;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        this.setContentView(R.layout.test_popwindow);  

        btn_open = (Button) this.findViewById(R.id.btn_open);  
        btn_open.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View arg0) {  
                showPopWindow();  
            }  
        });  

    }  

    private void showPopWindow() {  
        View view = LayoutInflater.from(this).inflate(R.layout.view_pop, null);  
        popview = new PopupWindow(view,  
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,  
                LayoutParams.WRAP_CONTENT, true);  
        popview.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  
        popview.setFocusable(true);  
        // Settings disappear when you click somewhere else  
        popview.setOutsideTouchable(true);  
        popview.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);  
        popview.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  
        popview.showAtLocation(findViewById(R.id.btn_open), Gravity.BOTTOM  
                | Gravity.CENTER_VERTICAL, 0, 0);  

    }  
}  


test_popwindow.xmlLayout file
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#ffffff"  
    android:orientation="vertical" >  

    <Button  
        android:id="@+id/btn_open"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:text="open" />  

</RelativeLayout>  


view_pop.xmlLayout file
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:orientation="vertical" >  

    <EditText  
        android:id="@+id/et_input"  
        android:layout_width="fill_parent"  
        android:layout_height="40dip"  
        android:singleLine="true"  
        android:textSize="16sp" >  
    </EditText>  

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/et_input"  
        android:layout_marginTop="20dip"  
        android:gravity="center_horizontal"  
        android:orientation="horizontal" >  

        <Button  
            android:id="@+id/confirm"  
            android:layout_width="80dip"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="30dip"  
            android:text="Determine" />  

        <Button  
            android:id="@+id/cancel"  
            android:layout_width="80dip"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="30dip"  
            android:text="cancel" />  
    </LinearLayout>  

</RelativeLayout>  

Posted by kevinritt on Thu, 07 May 2020 09:15:48 -0700