ScrollView中带有EditText的Android TextInputLayout隐藏了焦点上的提示

时间:2016-11-05 11:40:25

标签: android scrollview hint textinputlayout

如果我将一个EditText集中在ScrollView中,就会发生这种情况(很好): enter image description here

然后,我将焦点放在下面的EditText并从第一张图片重新聚焦EditText,然后发生这种情况: enter image description here

提示未显示或显示不正确(取决于滚动视图)。

部分解决方案: Scrollview看到我想要关注EditText而不是周围的android.support.design.widget.TextInputLayout元素。因此,它不关心是否显示提示。

如何解决? 我的XML(基本上是一个包含一些包含带有EditTexts的android.support.design.widget.TextInputLayout的linearlayouts的scrollview):

<ScrollView
            android:layout_width="match_parent"
            android:fadeScrollbars="false"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:weightSum="9"
                    android:descendantFocusability="beforeDescendants"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:text="1"
                        android:gravity="center"
                        android:layout_height="match_parent" />

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint1.1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:maxLines="1"
                            android:inputType="number"
                            android:hint="hint1.2"
                            android:layout_height="match_parent" />


                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="9"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:text="2"
                        android:gravity="center"/>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint2.1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint2.2"
                            android:inputType="number"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="9"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="3"/>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:hint="hint3.1"
                            android:maxLines="1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:hint="hint3.2"
                            android:maxLines="1"
                            android:inputType="number"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

1 个答案:

答案 0 :(得分:3)

使用此代码滚动到edittext视图的正确位置

your_scrollview.post(new Runnable() { 
@Override
 public void run() { 
       your_scrollview.smoothScrollTo(0, your_EditBox.getBottom());
 } 
});