【问题标题】:Android: How to push button above soft keyboardAndroid:如何按下软键盘上方的按钮
【发布时间】:2013-02-25 18:06:14
【问题描述】:

我有一个“保存”按钮,我想将它与软键盘一起向上推。因此,当用户在我的布局中单击 EditText 时,按钮必须保持在键盘上方。现在按钮隐藏在键盘下方。你是怎么做到的?

提前致谢!

【问题讨论】:

  • @CommonsWare 实际上,引用的文章在这里没有帮助。 OP 在其 EditText 字段下方有一个按钮(常见布局)。实际上没有办法使用标准输入法设置将 EditText 和 Button 向上推。不幸。
  • @DavidWasser:然后让 IME 的操作按钮和隐藏按钮做同样的事情。
  • @CommonsWare 如果还有一个复选框怎么办?那么IME的动作实际上是没有帮助的
  • 这个问题的答案需要更新才能使用 ConstraintLayout。

标签: java android xml android-layout android-softkeyboard


【解决方案1】:

您需要将键盘的输入模式设置为adjustResize。您可以在清单中将以下行添加到您的 Activity 属性中:

    android:windowSoftInputMode="adjustResize"

以下是在活动中添加的属性示例:

<activity 
     android:name=".activity.MyActivity"
     android:windowSoftInputMode="adjustResize">
</activity>

【讨论】:

【解决方案2】:

随着 Inthathep 的回答,您必须在父视图组中添加一个属性

android:fitsSystemWindows="true"

按需要工作。 即,在清单文件中,为活动添加

android:windowSoftInputMode="adjustResize"

和 例如。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:fitsSystemWindows="true" <!-- add this -->
    android:orientation="vertical"
    >
    <EditText
        android:id="@+id/et_assetview_comment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="80dp"
        android:background="@color/white"
        android:hint="Enter comments"
        />
    <Button
        android:id="@+id/btn_assetview_postcomment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POST"
        />
</LinearLayout>

【讨论】:

  • 只有这个有帮助。
  • 处理 xml 一直是个麻烦事。
【解决方案3】:

这样排列您的布局,您就可以将按钮放在键盘上方

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button_next"
        android:background="#0ff"
        >

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

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="250dp"
                android:hint="Hint"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ABC"
                android:textSize="50sp"
                />
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button_next"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:text="Button Next"
        />

</RelativeLayout>

在安卓清单中

<application
        ...
        >
        <activity android:name=".YourActivity"
            android:windowSoftInputMode="adjustResize"
           >
        </activity>
</application>

请注意,除了RelativeLayout,您还可以使用另一个ViewGroup,例如LinearLayout,带有权重,CordinatorLayout,...

【讨论】:

  • 你应该用一个巨大的文本来测试它,比如 15 行。您会看到,当编写它然后单击顶行时,edittext 的一部分将隐藏在键盘后面
  • 如果一切都是正确的,如果你给 windowFullscreen "true" 它将不起作用,所以只需从你的主题中删除一行:true
  • @varotariyavajsi 谢谢兄弟你节省了我的时间我被这个问题搞砸了非常感谢你
【解决方案4】:

所以这是一篇相当老的帖子,但我对所提供的答案感到很困惑。 oneavi 和 Intahep 都是正确的,但让我告诉你 android:windowSoftInputMode="adjustResize" 的确切位置。

在 Android 清单中

    <activity android:name=".DataScreen" />
    <activity android:name=".PauseScreen" />
    <activity android:name=".RouteInfo"
               android:windowSoftInputMode="adjustResize"> <!--This goes in the specific activity with the button -->
    </activity>

【讨论】:

  • 你应该用一个巨大的文本来测试它,比如 15 行。您会看到,当编写它然后单击顶行时,edittext 的一部分将隐藏在键盘后面
【解决方案5】:

最好的方法是隐藏击键,如有必要,按下键盘上方的按钮

 android:windowSoftInputMode="adjustResize|stateHidden"

【讨论】:

    【解决方案6】:

    补充一点 包括"adjustResize" 在内的上述任何一项都可以使用,除非您的活动是全屏的。那是我的情况。检查您的活动代码以确保其不是全屏显示。

    【讨论】:

      【解决方案7】:

      这个简单的设置使用键盘向上滚动整个布局。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
              <ImageView
                  android:id="@+id/image_iv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_above="@id/recycler_view"
                  android:layout_alignParentTop="true"
                  android:layout_centerHorizontal="true"
                  android:src="@drawable/image" />
      
              <androidx.recyclerview.widget.RecyclerView
                  android:id="@+id/recycler_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_above="@id/button"
                  android:layout_centerHorizontal="true" />
      
              <Button
                  android:id="@+id/button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_centerHorizontal="true"
                  android:text="Button" />
      
      </RelativeLayout>
      

      关键点:

      • 清单中没有添加任何内容。
      • 图像必须是“wrap_content”(不是固定大小),以便 android 根据需要调整其大小。
      • 所有视图必须如上所示链接。底视图 layout_alignParentBottom="true",顶视图 layout_alignParentTop="true",以及所有中间视图 layout_above="@id/view_below"。
      • layout_below="@id/view_above" 属性由于某种原因不起作用。

      【讨论】:

        【解决方案8】:

        在 AndroidX 中:

        使用CoordinatorLayout作为主要父布局并为您的内容添加NestedScrollView,并在CoordinatorLayout的子元素中添加您的布局或按钮以按下软键盘上方的按钮

        <androidx.coordinatorlayout.widget.CoordinatorLayout  
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             >
            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 android:isScrollContainer="true"  >
                .......
            </androidx.core.widget.NestedScrollView>
            <com.google.android.material.button.MaterialButton
                android:id="@+id/send_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="@string/login" />
        

        照片:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-25
          • 1970-01-01
          • 2012-05-31
          • 2019-05-18
          • 1970-01-01
          • 2013-05-15
          相关资源
          最近更新 更多