对于EditText,如何让键盘不覆盖它和其他一些视图?

For EditText, how do I let the keyboard not cover it and some other views?

背景

我有一个 EditText,当专注于它时,它应该显示键盘并显示整个 EditText,我可能需要在下面包含几个视图。

问题

由于某些要求,我需要 EditText 具有较小的文本大小,但也有最小的高度。

我制作了一个片段来作为这个问题的一个小例子:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false"
    android:clipToPadding="false" tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/messageEditText" android:layout_width="0px" android:layout_height="wrap_content"
        android:hint="write here" android:minHeight="?attr/actionBarSize" android:textSize="12sp"
        app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:background="" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我专注于 EditText 时,出于某种原因我看不到它的行:

在清单中,我有 android:windowSoftInputMode="adjustPan" 因为要求是移动东西而不是调整它们的大小。

我试过的

我尝试添加填充,并尝试添加一个假的底部可绘制对象。这些只能添加空 space,但会使该行显示得更远。

问题

是否可以让 EditText 具有最小高度和文本大小,同时还让它在键盘出现时显示行?

是否也可以在 EditText 下面显示更多内容?让 EditText 离键盘太近可不是什么好事。我知道在这个片段中我只有 EditText,但想象一下它下面会有更多视图。

使用AndroidManifest.xml文件中的代码:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" />

这段代码对我有用。 在 activity 标签中的清单文件中添加此属性:

android:windowSoftInputMode="adjustResize|stateHidden"

您可以在 android 清单中设置 windowSoftInputMode

<activity
android:windowSoftInputMode=["stateUnspecified",
                                       "stateUnchanged", "stateHidden",
                                       "stateAlwaysHidden", "stateVisible",
                                       "stateAlwaysVisible", "adjustUnspecified",
                                       "adjustResize", "adjustPan"] > 
</activity>

以你的情况,我觉得你应该用adjustPan。您的 window 大小没有改变,但内容会向上翻译。以下是文档中关于 adjustPan :

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

如果您不希望内容视图布局平移,这会使内容滚出屏幕。你可以使用 adjustResize

想了解更多可以看官方document

没有办法。键盘打开时只有 3 种反应方式:

1)什么都不做 2)调整大小 3)平移,使光标所在的行在屏幕上。

没有办法执行任何其他操作,也没有可靠的信号发送到应用程序显示键盘显示您可以响应。尤其是无法确保 EditText 底部的行会出现在屏幕上。

为确保这一点,您可以做的最好的事情就是调整大小,并仔细布局您的内容,以使调整大小对显示的 ui 的影响最小。但这可能可行也可能不可行,具体取决于 UI 的具体情况。而且您还没有向我们展示您正在尝试做的事情,因此我们无法就此提出建议。