![](/img/trans.png)
[英]how to set the soft keyboard push the data up in fragment in android?
[英]Android: How to push button above soft keyboard
我有一个“保存”按钮,我想将其与软键盘一起向上推。 因此,当用户在我的布局中单击 EditText 时,按钮必须保持在键盘上方。 现在按钮隐藏在键盘下方。 你怎么做到这一点?
提前致谢!
您需要将键盘的输入模式设置为adjustResize
。 您可以将以下行添加到清单中的活动属性中:
android:windowSoftInputMode="adjustResize"
这是在活动中添加的属性的示例:
<activity
android:name=".activity.MyActivity"
android:windowSoftInputMode="adjustResize">
</activity>
随着 Inththep 的回答,您必须在父视图组中添加一个属性
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 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
重的LinearLayout
, CordinatorLayout
,...
所以这是一个很老的帖子,但我对所提供的答案很挣扎。 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>
最好的方法是隐藏击键,必要时按下键盘上方的按钮
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" />
照片:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.