人造人。打开键盘时如何在键盘顶部显示按钮?



我需要显示我的按钮软键盘的顶部(当它被打开)。为此,我实现了这个https://medium.com/@madalinnita/android-how-to-move-views-above-keyboard-when-its- opene-quick -secure- solution90188c4d7b15。

我的布局结构:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:isScrollContainer="true"
app:layout_constraintBottom_toTopOf="@+id/bottomContainer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText..../>
<EditText..../>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:gravity="end"/>
</androidx.constraintlayout.widget.ConstraintLayout>

一切都很好,但在小型设备上,我的按钮覆盖了编辑文本。有可能修好吗?我没有任何想法。我需要以某种方式使按钮和键盘出现在编辑文本下,即使在小型设备上。

请帮帮我。

你可以将整个视图包装在ScrollView中,这样你就可以在键盘打开时滚动视图。在这个例子中,我使用的是LinearLayout,你也可以使用其他的layout。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
</ScrollView>

并将这行代码添加到特定活动的Manifest中:

android:windowSoftInputMode="adjustResize"

添加一行->android:windowSoftInputMode="adjustResize",在Manifest中的当前活动标签中,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
...>

<activity 
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
...
/>
...
</application>
</manifest>

最新更新