Android:如何按下软键盘上方的按钮

Xan*_*der 46 java xml android android-layout android-softkeyboard

我有一个"保存"按钮,我想用软键盘一起推.因此,当用户单击布局中的EditText时,该按钮必须保持在键盘上方.现在按钮隐藏在键盘下方.你怎么做到这一点?

提前致谢!

Int*_*hep 93

    android:windowSoftInputMode="adjustResize"
Run Code Online (Sandbox Code Playgroud)

试试这个

  • 我正在使用 ConstraintLayout,它对我不起作用。 (9认同)

one*_*avi 29

与Inthathep的答案一起,您必须在父视图组中添加属性

android:fitsSystemWindows="true"
Run Code Online (Sandbox Code Playgroud)

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

android:windowSoftInputMode="adjustResize"
Run Code Online (Sandbox Code Playgroud)

例如.

<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>
Run Code Online (Sandbox Code Playgroud)

  • 处理 xml 一直是个麻烦事。 (5认同)
  • 只有这有帮助。 (2认同)

Pha*_*inh 13

像这样订购您的布局,您就可以将按钮放在键盘上方

<?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>
Run Code Online (Sandbox Code Playgroud)

在android清单中

<application
        ...
        >
        <activity android:name=".YourActivity"
            android:windowSoftInputMode="adjustResize"
           >
        </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意RelativeLayout,您可以使用另一个,ViewGroup例如LinearLayout重量CordinatorLayout,...

  • 万一一切都正确,如果你给 windowFullscreen "true" 它将不起作用,所以只需从你的主题中删除一行:&lt;item name="android:windowFullscreen"&gt;true&lt;/item&gt; (2认同)

小智 7

所以这是一个相当古老的帖子,但我在提供的答案上挣扎.oneavi和Intahep都是正确的,但让我告诉你哪里android:windowSoftInputMode="adjustResize"去了.

在Android Manifest中

    <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>
Run Code Online (Sandbox Code Playgroud)