简体   繁体   English

Android如何在单击editText时使scrollView自动滚动到底部?

[英]Android how to make scrollView auto scroll to the bottom when click on editText?

I show you guy the normal view of the layout below: 我向您展示以下布局的正常视图:

Image 1 图片1

When I clicked on email editText the softkeyboard was pop up, the editText is cover by softkeyboard, it that show as picture below: 当我单击电子邮件editText时 ,弹出软键盘 ,editText被软键盘覆盖,如下图所示:

Image 2 图片2

So, I wanted to click the editText and the scrollView will auto scroll to the bottom and not cover by softkeyboard that show as picture below: 因此,我想单击editText,scrollView将自动滚动到底部,而不是被软键盘覆盖,如下图所示:

Image 3 图片3

mylayout.xml code below: mylayout.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="@color/bg_account_light_brown"
android:clickable="true">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/scroll"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/btn_login">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="1.0">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/bg_carousel_01" />

        <EditText
            android:id="@+id/i_email"
            android:layout_width="320dp"
            android:layout_height="49dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/rounded_shape_input"
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.493"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView" />


        <TextView
            android:id="@+id/textViewSignUp"
            android:layout_width="284dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="300dp"
            android:fontFamily="@font/lato_bold"
            android:gravity="center"
            android:text="LOGIN"
            android:textColor="@color/white"
            android:textSize="70sp"
            android:textStyle="bold|bold"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView" />

        <EditText
            android:id="@+id/i_password"
            android:layout_width="320dp"
            android:layout_height="49dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/rounded_shape_input"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.493"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/i_email"
            app:layout_constraintVertical_bias="0.52" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>

<Button
    android:id="@+id/btn_login"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/rounded_shape2"
    android:clickable="true"
    android:text="Login"
    android:textColor="#FFFFFF"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.973" />

</android.support.constraint.ConstraintLayout>

Try this use make your ScrollView android:fillViewport="true" 尝试使用此方法使ScrollView android:fillViewport="true"

android:fillViewport="true"

Defines whether the scrollview should stretch its content to fill the viewport. 定义滚动视图是否应拉伸其内容以填充视口。

and in your manifest file add android:windowSoftInputMode="adjustResize" in your login activity 并在清单文件中的登录活动中添加android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen 活动的主窗口始终会调整大小,以便为屏幕上的软键盘腾出空间

<activity
    android:name=".activities.LoginActivity"
    android:windowSoftInputMode="adjustResize"/>

Add in Your Scrollview : 在您的Scrollview添加:

android:fillViewport="true"

And Add in Your Manifest File : 并添加Manifest文件:

android:windowSoftInputMode="adjustResize"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM