Android:PopupWindow填充键盘上方的空间

时间:2013-10-22 07:31:58

标签: android android-layout android-view

我在scrollView中有一个按钮。单击该按钮时,我需要一个PopupWindow,只要单击该按钮就会显示软键盘。这就是我的代码今天的样子:

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>

当我尝试fill_parent而不是wrap_content时,它会通过填充整个屏幕开始。我正在努力实现这样的目标:enter image description here

2 个答案:

答案 0 :(得分:3)

也许尝试使用以下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>

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

答案 1 :(得分:1)

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

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

                }
            });

然后您可以使用键盘的高度和设备的宽度加载弹出窗口:

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

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

}