软键盘覆盖了EditText下面的TextView。

3
我在EditText下方添加了一个TextView来显示该EditText的字符数。当EditText未处于焦点状态时,它看起来像这样。

enter image description here

当EditText处于焦点状态时:

enter image description here

如您所见,“7/500”被TextView盖住了。
我的activity的XML代码如下:
<ScrollView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp"
    android:paddingTop="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <ImageView
            android:id="@+id/display_photo"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"/>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/caption_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/et_start_photo_story_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:gravity="bottom"
                android:maxLength="500"
                android:paddingBottom="15dp"
                android:privateImeOptions="nm"
                android:textColor="@color/black" />
        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/tv_character_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textColor="@color/grey700"
            android:textSize="14sp" />
    </LinearLayout>
</ScrollView>

我在清单文件中有这么一行代码:android:windowSoftInputMode="adjustPan"。我试着改变ImageView的高度,但是当EditText处于焦点状态时,字符计数仍然被隐藏。


请在清单文件中使用android:windowSoftInputMode="adjustResize",这样您就可以滚动视图了。 - Saurabh Vadhva
请将 tv_character_count 放置在 caption_layout 的右侧(而不是下方),或者使用 RelativeLayout。看看您是否能够实现这一点。 - Amrut Bidri
2个回答

2

不要设置为

android:windowSoftInputMode="adjustPan"

尝试设置为

android:windowSoftInputMode="adjustResize"

这样当edittext被聚焦时,您的视图将变得可滚动。


这就是我最初的想法,但它会切掉照片。有什么解决办法吗? - Bargain23
请尝试将您的ScrollView添加到父RelativeLayout中。 - Shrikant
@Bargain23 只有3种模式可以响应键盘的出现。adjustPan(如果需要确保光标在屏幕上,则会滚动您的应用程序),adjustResize(将调整您的应用程序大小以适合键盘上方的空间)和adjustNone(不执行任何操作)。如果您有很多空白处,可以使用相对布局的技巧来减少它,但最终必须做出某些让步-现在您的空间较少,某些内容将无法适应。 - Gabe Sechan
@GabeSechan 有没有办法让 TextView 始终浮在软键盘上方?或者欺骗软键盘,使其将其视为 EditText 的一部分? - Bargain23
@Bargain23 对于这两个问题都是否定的。这不是谷歌实现的一部分。而且,没有可靠的方法可以告诉你软键盘已经打开,以便自己滚动。 - Gabe Sechan

0
如果您正在使用依赖注入框架,例如com.google.dagger:hilt-android,请将android:windowSoftInputMode="adjustPan|adjustResize"放置在清单文件的应用程序部分,而不是活动部分。

为什么依赖注入框架会影响这个? - Ryan M
我还没有进行完整的分析,但在将依赖注入引入现有项目(需要扩展Application类)后,android:windowSoftInputMode直到我将其移动到应用程序部分才生效。我认为这个观察结果可以节省其他人的时间。 - Stephen kamanu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接