我有一个“保存”按钮,我想和软键盘一起向上推。因此,当用户点击我的布局中的EditText时,该按钮必须保持在键盘上方。现在该按钮被隐藏在键盘下面了。你如何做到这一点?
谢谢!
谢谢!
您需要将键盘的输入模式设置为adjustResize
。您可以通过在清单中的活动属性中添加以下行来实现:
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="adjustResize"
这是一个在活动中添加了属性的示例:
<activity
android:name=".activity.MyActivity"
android:windowSoftInputMode="adjustResize">
</activity>
除了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>
按照这种方式排列你的布局,你将能够将按钮放在键盘上方。
<?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
(例如带有权重的LinearLayout
,CordinatorLayout
等)来代替 RelativeLayout
。
这是一篇相当旧的文章,但我对提供的答案感到困惑。一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>
最佳方法是隐藏按键操作,必要时按下键盘上方的按钮。
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>
要点:
在 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" />
照片: