当用户按下并出现键盘时,EditText 消失

EditText disappears when user press on and keyboard appear

我在 EditText 中有 3 个表单,我需要在用户单击表单和出现键盘时自动重新调整位置,在我的 manifest.xml 我添加了这一行: " android:windowSoftInputMode="stateHidden|adjustResize" "

这是我的XML屏幕代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_farm"

    >

    <Button
        android:id="@+id/buttonLogar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="136dp"
        android:background="@drawable/bgbuttonform"
        android:drawableRight="@drawable/ic_check_black_24dp"
        android:outlineAmbientShadowColor="@android:color/black"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="@string/confirmar_dados"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextPassword" />

    <Button
        android:id="@+id/btn_selecionar_foto"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="100dp"
        android:background="@drawable/bg_btn_rounded_img"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:text="Selecione uma foto"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextEmailUser"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint=" Digite aqui seu email"
        android:inputType="textEmailAddress"
        android:paddingLeft="10dp"
        app:layout_constraintBottom_toTopOf="@+id/editTextPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextUser" />

    <EditText
        android:id="@+id/editTextUser"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint="Digite aqui seu nome"
        android:inputType="textPersonName"
        android:paddingLeft="10dp"
        app:layout_constraintBottom_toTopOf="@+id/editTextEmailUser"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_selecionar_foto" />

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint="Digite aqui sua senha"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        app:layout_constraintBottom_toTopOf="@+id/buttonLogar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextEmailUser" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imageFotoUser"
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:civ_border_width="1dp"
        app:civ_border_color="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btn_selecionar_foto"
        app:layout_constraintEnd_toEndOf="@+id/btn_selecionar_foto"
        app:layout_constraintStart_toStartOf="@+id/btn_selecionar_foto"
        app:layout_constraintTop_toTopOf="@+id/btn_selecionar_foto"
         />

    <TextView
        android:id="@+id/textViewInicial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="175dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="178dp"
        android:layout_marginBottom="33dp"
        android:text="Seja bem vindo(a) ao Agro Lucros!\nPreencha o formulário abaixo para seguir"
        android:typeface="serif"
        android:textSize="18.5dp"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/btn_selecionar_foto"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textJaTemConta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="94dp"
        android:text="Já possui uma conta? Clique aqui!"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:fontFamily="sans-serif-black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonLogar" />

</androidx.constraintlayout.widget.ConstraintLayout>

我基本上需要在键盘出现时编辑文本向上滚动到屏幕,抱歉我的解释。

尝试在您的清单中添加 android:windowSoftInputMode="adjustPan|adjustResize",如下所示。因为我认为您缺少 adjustPan 属性。

<activity
            android:name=".activityname"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

您也可以将编辑文本包含在 ScrollView.

只是添加@dmak2709 上面的建议。

我建议您对所有布局使用 NestedScrollView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

 <Button
        android:id="@+id/buttonLogar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="136dp"
        android:background="@drawable/bgbuttonform"
        android:drawableRight="@drawable/ic_check_black_24dp"
        android:outlineAmbientShadowColor="@android:color/black"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="@string/confirmar_dados"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextPassword" />

    <Button
        android:id="@+id/btn_selecionar_foto"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="100dp"
        android:background="@drawable/bg_btn_rounded_img"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:text="Selecione uma foto"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextEmailUser"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint=" Digite aqui seu email"
        android:inputType="textEmailAddress"
        android:paddingLeft="10dp"
        app:layout_constraintBottom_toTopOf="@+id/editTextPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextUser" />

    <EditText
        android:id="@+id/editTextUser"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint="Digite aqui seu nome"
        android:inputType="textPersonName"
        android:paddingLeft="10dp"
        app:layout_constraintBottom_toTopOf="@+id/editTextEmailUser"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_selecionar_foto" />

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/bg_editextrounded"
        android:ems="10"
        android:hint="Digite aqui sua senha"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        app:layout_constraintBottom_toTopOf="@+id/buttonLogar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextEmailUser" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imageFotoUser"
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:civ_border_width="1dp"
        app:civ_border_color="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btn_selecionar_foto"
        app:layout_constraintEnd_toEndOf="@+id/btn_selecionar_foto"
        app:layout_constraintStart_toStartOf="@+id/btn_selecionar_foto"
        app:layout_constraintTop_toTopOf="@+id/btn_selecionar_foto"
         />

    <TextView
        android:id="@+id/textViewInicial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="175dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="178dp"
        android:layout_marginBottom="33dp"
        android:text="Seja bem vindo(a) ao Agro Lucros!\nPreencha o formulário abaixo para seguir"
        android:typeface="serif"
        android:textSize="18.5dp"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/btn_selecionar_foto"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textJaTemConta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="94dp"
        android:text="Já possui uma conta? Clique aqui!"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:fontFamily="sans-serif-black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonLogar" />

 </LinearLayout>



</android.support.v4.widget.NestedScrollView>

如果您想了解更多信息,请查看此 page

希望对您有所帮助。