首页 > 解决方案 > 键盘打开时如何向上滑动活动?

问题描述

键盘与登录页面上的电子邮件 EditText 冲突。我想在键盘打开时在欢迎回来和描述文本上方滑动。

当前状态:https ://i.hizliresim.com/135sizd.png 我想要什么:https ://i.hizliresim.com/pgacbn2.png

我使用了adjustpan和adjustresize,但没有用。我在等你的帮助。谢谢你。

这是我的 XML 代码。

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout 
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:padding="15dp"
android:background="@color/main_bg"
tools:context=".ui.auth.LoginActivity">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="true">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:fontFamily="@font/i_d_semibold"
            android:letterSpacing="-0.001"
            android:text="@string/login_header"
            android:textColor="@color/header"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/i_d_light"
            android:text="@string/login_description"
            android:textColor="@color/description"
            android:textSize="14sp"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/input_bg"
            android:drawableStart="@drawable/mail_ic"
            android:drawableEnd="@drawable/check_ic"
            android:drawablePadding="20dp"
            android:fontFamily="@font/i_d_regular"
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:textColor="@color/header"
            android:textColorHint="@color/description"
            android:textSize="14sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/input_bg"
            android:drawableStart="@drawable/password_ic"
            android:drawableEnd="@drawable/eye_ic"
            android:drawablePadding="20dp"
            android:fontFamily="@font/i_d_regular"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:textColor="@color/header"
            android:textColorHint="@color/description"
            android:textSize="14sp" />



    </LinearLayout>

</androidx.core.widget.NestedScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="250dp"
        android:textSize="13sp"
        android:layout_height="wrap_content"
        android:text="@string/terms_of_service"
        android:textColor="@color/description"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_marginBottom="10dp"
        android:gravity="center" >

        <com.google.android.material.button.MaterialButton
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            app:cornerRadius="10dp"
            android:text="@string/get_started"
            android:textColor="@color/white"
            android:layout_weight="0.45"
            app:strokeColor="@color/white"
            android:fontFamily="@font/i_d_semibold"
            android:textAllCaps="false"
            android:letterSpacing="-0.001"
            app:strokeWidth="2dp"
            app:backgroundTint="@color/main_bg"
            />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.02" />

        <com.google.android.material.button.MaterialButton
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            app:cornerRadius="10dp"
            android:layout_weight="0.45"
            android:text="@string/login"
            android:fontFamily="@font/i_d_bold"
            android:textAllCaps="false"
            android:letterSpacing="-0.001"
            app:backgroundTint="@color/main_button"
            />

    </LinearLayout>

</LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

标签: androidxmlandroid-studio

解决方案


加入AndroidManifest.xml

android:windowSoftInputMode="adjustPan|adjustResize"

更改您onCreate() methodactivity class类似内容


getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

推荐阅读