显示软键盘时将视图向上平移一点的好方法是什么

What is a good way to pan view a little more up when soft keyboard is shown

我有一个 activity 设置了 "adjustPan" 选项,当 EditText 获得焦点并显示软键盘时,它可以很好地平移我的视图,但我的问题是有UI 我的 EditText 下面的元素需要在显示键盘时也可见,而不是在较小的屏幕上。我可以通过将视图链接到 EditText 或其他东西来告诉视图始终平移以便那些 UI 元素可见,还是我必须手动控制平移并计算高度和所有元素,或者是什么这里有好的解决方案吗?

在 Android 您需要的 class 清单文件中使用 stateHidden ..

android:windowSoftInputMode="stateHidden"

否则,

在Activity,

getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

您可以使用 ViewTreeObserver 将您的内容平移更多一点。 这是我的应用程序。

    int edtHeight = 0;

    // llContent is the Viewgroup layout which is inside a ScrollView.
    ViewTreeObserver viewTreeObserver1 = llContent.getViewTreeObserver();
    viewTreeObserver1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            // Here edtHeight will get the height of edittext as i wanted my view to scroll that much bit more
            edtHeight = edtTemprature.getHeight();

            ViewTreeObserver obs = llContent.getViewTreeObserver();

            obs.removeGlobalOnLayoutListener(this);
        }
 });

//llMain is the parent View Group of my XML layout 
    llMain.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
            @Override
            public void onGlobalLayout()
            {
                Rect r = new Rect();
                // r will be populated with the coordinates of your view
                // that area still visible.
                llMain.getWindowVisibleDisplayFrame(r);

                int heightDiff = llMain.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100)
                { // if more than 100 pixels, its probably a keyboard...
                    if(edtTemprature.hasFocus()){
                        llContent.scrollTo(0, (int) (edtHeight * 1.5));
                    }else{
                        llContent.scrollTo(0, 0);
                    }
                }
                else
                {
                    llContent.scrollTo(0, 0);
                }
            }
        });