如何在不更改键盘视图的情况下使用ScrollView?

时间:2019-05-17 12:45:44

标签: java android scrollview

我的一个视图中的空间有限,并且我正在尝试实现一个滚动视图,该视图可以帮助解决空间有限的问题。但是,当我尝试实现滚动视图时,会弄乱键盘,每次我现在尝试编辑EditText时,都会导致视图中的元素随键盘向上移动。

还有其他可以尝试实现的解决方案或解决方法吗?

我在Move layouts up when soft keyboard is shown?处尝试过该解决方案,建议将以下代码添加到清单android:windowSoftInputMode="stateHidden|adjustResize中,以防止键盘在运行时跳起。

我还尝试设置滚动视图的重力,并使用不同的布局示例:线性,约束,相对布局。我试图制作一个自定义键盘类,并通过在相关的视图活动类中设置它来实现它(不是我的代码,而是试图实现它)

Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();

public CommentKeyBoardFix(Activity activity)
{
    FrameLayout content = activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}

private void possiblyResizeChildOfContent()
{
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious)
    {
        int heightDifference = 0;
        if (heightDifference > (usableHeightNow /4))
        {
            // keyboard probably just became visible
            frameLayoutParams.height = usableHeightNow - heightDifference;
        }
        else
        {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightNow;
        }
        mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight()
{
    mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
    return contentAreaOfWindowBounds.height();
}
}

And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java

这会产生一个静态成员错误,我不知道该如何解决。

但是这些都不能解决问题。

导致错误的我当前的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/main_layout1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/freq_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Freq"
                android:textSize="14sp" />

            <EditText
                android:id="@+id/freq"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:digits="0123456789.,"
                android:inputType="numberDecimal"
                android:textAlignment="center"
                android:textColor="@color/black" />

            <Spinner
                android:id="@+id/spinner_r1_unit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:tooltipText="unit"

                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">

            <TextView
                android:id="@+id/duty_cycle_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/duty_cycle"
                android:textSize="14sp"
                android:layout_marginLeft="5dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/duty_cycle_percentage"
                android:layout_marginLeft="5dp"
                android:text="50%"/>


        </LinearLayout>

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:max="100"
            android:progress="50" />

        <android.support.v7.widget.AppCompatButton
            android:layout_width="100dp"
            android:layout_below="@id/main_layout1"
            android:layout_height="wrap_content"
            android:text="Design"
            android:layout_marginLeft="30dp"
            android:id="@+id/button_design"
            android:layout_marginTop="10dp"
            android:background="@drawable/white_rounded_button"
            />

        <ScrollView
            android:layout_width="200dp"
            android:layout_height="100dp"
            >

        </ScrollView>

    </LinearLayout>



</merge>

但是,如果我删除滚动视图,则可以解决此问题,我需要添加滚动视图,因为此.xml文件用作空间非常有限的另一个.xml文件中的代码段。

滚动视图的实现是否还有其他可能的解决方法?是否有任何很好的解释来说明为什么此滚动视图会导致此错误,并考虑到空间问题,我还可以考虑其他哪些替代方法,这些替代方法可以提供与滚动视图类似的结果,而不会导致键盘错误? / p>

0 个答案:

没有答案