Android:如何在软键盘上方放置按钮

80
我有一个“保存”按钮,我想和软键盘一起向上推。因此,当用户点击我的布局中的EditText时,该按钮必须保持在键盘上方。现在该按钮被隐藏在键盘下面了。你如何做到这一点?
谢谢!

1
http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html - CommonsWare
@CommonsWare实际上,参考文章在这里没有帮助。 OP在他的EditText字段下面有一个按钮(常见的布局)。实际上,使用标准输入法设置无法推动EditText 和Button。不幸的是。 - David Wasser
1
@DavidWasser:然后让输入法的动作按钮执行与隐藏按钮相同的操作。 - CommonsWare
@CommonsWare 如果还有一个复选框怎么办?那么 IME 的操作就没有帮助了,实际上。 - Antek
这个问题的答案需要更新以适应ConstraintLayout的工作。 - SMBiggs
8个回答

133

您需要将键盘的输入模式设置为adjustResize。您可以通过在清单中的活动属性中添加以下行来实现:

android:windowSoftInputMode="adjustResize"
    android:windowSoftInputMode="adjustResize"

这是一个在活动中添加了属性的示例:

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

1
成功了!但是并不完全符合预期。请查看这个新问题:https://dev59.com/AGUp5IYBdhLWcg3wVWiv - Xander
13
我正在使用ConstraintLayout,但它对我不起作用。 - Karan Sharma

51

除了Inthathep的回答之外,您还需要在父ViewGroup中添加一个属性。

android:fitsSystemWindows="true"

按照预期工作。即,在清单文件中,为活动添加

android:windowSoftInputMode="adjustResize"

例如(eg.)

<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>

2
只有这个有用。 - Rohan Kandwal
9
处理 XML 一直是件很烦的事情。 - filthy_wizard

27

按照这种方式排列你的布局,你将能够将按钮放在键盘上方。

<?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>

输入图像描述

请注意,您可以使用其他 ViewGroup(例如带有权重的LinearLayoutCordinatorLayout等)来代替 RelativeLayout


你应该使用一个大文本进行测试,比如15行。然后你会发现当你写作并点击顶部一行时,EditText的一部分会被键盘遮挡住。 - Evgenii Vorobei
3
如果一切都正确,而你给了windowFullscreen参数"true",它将不起作用,所以只需从你的主题中删除一行:<item name ="android:windowFullscreen">true</item>。请注意不要改变原文的意思,使翻译更加通俗易懂。 - varotariya vajsi
@varotariyavajsi 谢谢兄弟,你帮我省了不少时间。我之前一直被这个问题困扰,非常感谢你。 - MustafaShaikh

11

这是一篇相当旧的文章,但我对提供的答案感到困惑。一avi和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的一部分会被键盘遮挡住。 - Evgenii Vorobei

4

最佳方法是隐藏按键操作,必要时按下键盘上方的按钮。

 android:windowSoftInputMode="adjustResize|stateHidden"

2

额外说明 上述所有内容,包括"adjustResize"都可以使用,除非你的活动是全屏的。这就是我的情况。检查你的活动代码,确保它不是全屏的。


1
这个简单的设置可以使用键盘将整个布局向上滚动。
<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" 属性出现问题,原因未知。

0

在 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" />

照片:

https://snipboard.io/n45tbx.jpg


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