【问题标题】:What is a good way to pan view a little more up when soft keyboard is shown显示软键盘时,有什么好方法可以将视图向上平移一点
【发布时间】:2015-02-17 06:56:40
【问题描述】:

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

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你可以做的是你可以使用 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);
                    }
                }
            });
    

    【讨论】:

    • 是的,这也是我唯一的想法,希望有更聪明的方法
    • 我也试图找到更好的方法,但最终使用了这个@Neigaard
    【解决方案2】:

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

    android:windowSoftInputMode="stateHidden"

    否则,

    在活动中,

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

    【讨论】:

    • 不,我确实想要键盘,但我想控制它在我的视野中平移多远/多长时间
    • 在我的程序中,我使用 stateHidden 和 android:windowSoftInputMode="adjustPan" 来填充编辑文本。它工作正常..
    • 完全错了,stateHidden 向 Activity 指示不应显示软键盘。默认(您必须手动设置焦点才能显示它),它对平移长度或其他任何事情都没有任何作用 - 这是一个错误的答案。
    猜你喜欢
    • 2010-12-30
    • 2018-12-16
    • 1970-01-01
    • 2015-12-10
    • 2013-08-10
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多