【问题标题】:Keyboard hides EditText键盘隐藏 EditText
【发布时间】:2019-10-15 18:03:37
【问题描述】:

我正在编写一个有聊天功能的应用程序,它看起来如下:

由于某种原因,键盘隐藏了EditText 的下部。此外,它似乎隐藏了聊天中的最后 2 条消息。

XML如下:

<?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"
    tools:context=".ChatActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_Messages"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintBottom_toBottomOf="parent">

        <EditText
            android:id="@+id/et_Message"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:background="@drawable/et_rounded"
            android:fontFamily="@font/assistant"
            android:hint="Type a message"
            android:textSize="12sp"
            android:paddingLeft="24dp"
            android:inputType="textMultiLine|textPersonName"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.85"/>

        <ImageButton
            android:id="@+id/ib_Send"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginHorizontal="8dp"
            style="?android:borderlessButtonStyle"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_path_2830"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintWidth_percent="0.1"/>


    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

有什么原因吗?

在我的清单中,我有:

<activity android:name=".ChatActivity" android:windowSoftInputMode="adjustPan"/>

编辑:据我观察,键盘完全打开到 EditText 内文本的底部。由于该文本位于 EditText 的中间,因此它似乎隐藏了较低的灰色部分。所以据我了解,它需要从 EditText 外部的底部开始,就像从 EditText 的外部形状开始,而不是从文本开始的地方开始。

例如从这里开始;

代替:

据我了解,我需要键盘上的“下边距”之类的东西。

只是说清楚 - 我知道AdjustResize/AdjustPanAdjustResize 缩小了我的 EditText 所以我试图避免它。 AdjustPan 完成了这项工作,但是它将屏幕移动到了文本的底部!文字的底部不是EditText 的底部。这就是我想要实现的目标,所以AdjustPan 不仅仅解决了问题。添加marginBottom 也不能解决问题,因为它只是让AdjustPan 移动屏幕再次移动到文本底部。我试图“愚弄”AdjustPan 或添加一些偏见。

谢谢

【问题讨论】:

  • 请您尝试为顶级约束布局提供一些边距底部值,例如 16dp
  • 我尝试添加:android:layout_marginBottom="16dp 但它没有工作伙伴。
  • 你试过把 android:windowSoftInputMode="adjustPan" 改成 android:windowSoftInputMode="adjustResize"
  • 嗨@Ben,请您与我们分享您的解决方案

标签: android xml android-layout android-softkeyboard


【解决方案1】:

在您的清单中将其放入您的特定活动中:

 android:windowSoftInputMode="adjustResize|stateHidden"

【讨论】:

    【解决方案2】:

    将此添加到您的活动中:

    android:windowSoftInputMode="adjustPan"
    

    或者这个:

    android:windowSoftInputMode="adjustResize|stateHidden"
    

    【讨论】:

    • 正如我在问题中所写的,这一行已经存在于我的代码中。它不起作用。它位于我的 EditText 中文本的底部,但由于文本的下部和 EditText 的形状的下部不一样,所以看起来它隐藏了它。
    【解决方案3】:

    尝试在您的清单中使用android:windowSoftInputMode="adjustResize"

    【讨论】:

    • AdjustResize 确实使键盘低于edittext,但是,EditText 的大小缩小到一个很小的edittext。
    【解决方案4】:

    在你的清单中放

      android:windowSoftInputMode="adjustResize"
    

    此外,如果仅添加上述内容并不能解决您的问题,或者,请尝试为您的编辑文本或放置编辑文本的布局提供“下边距”。

    我们说

     android:layout_marginBottom="10dp"
    

    您还可以将编辑文本放入布局中并在其周围留出边距。

    【讨论】:

      【解决方案5】:

      您采用了两个约束布局,并将整个屏幕按 90:10 的比例划分。 如果这个固定比例没有限制,那么您可以将比例更改为 85:15。

       app:layout_constraintHeight_percent="0.85"
       app:layout_constraintHeight_percent="0.15"
      

      完整的 EditText 在后一种情况下可见。

      【讨论】:

      • 我已将其更改为 0.6,0.4 以进行检查。键盘只是隐藏 EditText,直到文本在 EditText 内开始。
      【解决方案6】:

      你把所有的编辑文本放在一个linearlayoutvertical orientation 中,这个lineralayout 放在scroolview 下 比如前。

      <scroolview>
        <linearlayout>
       ---text view your all---
        </linearlayout>
      <scroolview>
      

      【讨论】:

        【解决方案7】:

        我也有同样的问题。试试下面的代码:

        android:windowSoftInputMode="adjustPan|stateHidden"

        在您的AndroidManifest.xml 中删除android:windowSoftInputMode 的adjustPan 值。

        <activity
                android:name=".Activities.InputsActivity"
                ...
                android:windowSoftInputMode="adjustResize|stateHidden"
                />
        

        adjustPan值在Android官网上解释为:

        活动的主窗口未调整大小以为软体腾出空间 键盘。而是自动平移窗口的内容 这样当前的焦点就不会被键盘和用户遮挡 总能看到他们在输入什么。这通常不太理想 而不是调整大小,因为用户可能需要关闭软键盘才能 到达窗口的模糊部分并与之交互。

        来源: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

        【讨论】:

          猜你喜欢
          • 2014-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-13
          • 2015-09-29
          • 1970-01-01
          • 1970-01-01
          • 2013-12-06
          相关资源
          最近更新 更多