Android 安卓:弹出窗口填充键盘上方的空间

Android 安卓:弹出窗口填充键盘上方的空间,android,android-layout,android-view,Android,Android Layout,Android View,我在scrollView中有一个按钮。单击该按钮时,我需要一个弹出窗口,以便在单击该按钮后立即显示软键。这就是我的代码今天的样子: final Button b = (Button) findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

我在scrollView中有一个按钮。单击该按钮时,我需要一个弹出窗口,以便在单击该按钮后立即显示软键。这就是我的代码今天的样子:

final Button b = (Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popup, null);
                final PopupWindow popupWindow = new PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,true);
                popupWindow.showAtLocation(v.getRootView(), Gravity.FILL, 0, 0);

                //Bring soft keyboard up : NOT WORKING
                final InputMethodManager mInputMethodManager = (InputMethodManager) getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                EditText editText = (EditText) popupView.findViewById(R.id.editText);
                mInputMethodManager.showSoftInput(editText, 0);

            }
        });
我的布局XML如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:hint="Your Views"
        >
    </EditText>



</RelativeLayout>


当我尝试填充父项而不是包装内容时,它首先填充整个屏幕。我正在尝试实现这样的目标:

您可以像这样获得键盘的高度:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:hint="Your Views"
        >
    </EditText>



</RelativeLayout>
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    loadDialog(heightDifference);

                }
            });
然后,您可以加载带有键盘高度和设备宽度的popupwindow:

public void loadDialog(int height)
{
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width  = mDisplay.getWidth();

        //load the popup window with the height and width...

}

您可以像这样获得键盘的高度:

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    loadDialog(heightDifference);

                }
            });
然后,您可以加载带有键盘高度和设备宽度的popupwindow:

public void loadDialog(int height)
{
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width  = mDisplay.getWidth();

        //load the popup window with the height and width...

}

或许可以尝试使用以下xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:isScrollContainer="true" >


    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:hint="Your Views" />

</RelativeLayout>


它会自动设置布局的高度,直到软键盘开始。

或许可以尝试使用以下xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:isScrollContainer="true" >


    <EditText
        android:id="@+id/yourViewsEditText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:hint="Your Views" />

</RelativeLayout>


它会自动设置布局的高度,直到软键盘开始。

现在看起来怎么样?现在看起来怎么样?太棒了。。谢谢Waqas。这样做是为了打开键盘:popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u始终可见);设置输入方法模式(需要输入方法);令人惊叹的。。谢谢Waqas。这样做是为了打开键盘:popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u始终可见);设置输入方法模式(需要输入方法);