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

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

先谢谢你!

77885 次浏览

您需要将键盘的输入模式设置为 adjustResize。您可以这样做,将以下行添加到清单中的活动属性中:

    android:windowSoftInputMode="adjustResize"

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

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

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

因此,这是一个相当老的职位,但我挣扎的答案提供。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>

像这样排列你的布局,你就可以把按钮放在键盘上面

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

enter image description here

请注意,与 RelativeLayout不同,您还可以使用另一个 ViewGroup,如带有重量的 LinearLayoutCordinatorLayout、 ..。

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

 android:windowSoftInputMode="adjustResize|stateHidden"

补充一点 除非你的活动是全屏的,以上任何一个包括 "adjustResize"都可以工作。那是我的案子。检查您的活动代码,以确保它不在全屏幕。

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

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

要点:

  • 清单上没有增加任何东西。
  • 图像必须是“ wra _ content”(不是固定大小) ,以允许 android 根据需要调整图像大小。
  • 所有视图必须如上所示进行链接。底部视图晒 _ alignParentbottom = “ true”,顶部视图晒 _ alignParentTop = “ true”,以及所有中间视图晒 _ above = “@id/view _ under”。
  • 由于某些原因,laydown = “@id/view _ above”属性无法工作。

在 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