当显示软键盘时,有什么好方法可以平移视图

时间:2015-02-17 06:56:41

标签: android android-layout

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

2 个答案:

答案 0 :(得分:0)

您可以使用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);
                }
            }
        });

答案 1 :(得分:-2)

在Android manifestfile中使用stateHidden,你需要哪个类..

机器人:windowSoftInputMode =" stateHidden"

或者,

在活动中,

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