Android 如何让scrollView在点击editText时自动滚动到底部?
Android how to make scrollView auto scroll to the bottom when click on editText?
我给你们看下面布局的正常视图:
Image 1
当我点击email editText时弹出软键盘,编辑文本被软键盘覆盖,如下图所示:
Image 2
所以,我想点击 editText,scrollView 会自动滚动到底部,而不是被软键盘覆盖,如下图所示:
Image 3
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>
试试这个用途让你的 ScrollView
android:fillViewport="true"
Defines whether the scrollview should stretch its content to fill the viewport.
并在您的清单文件中添加 android:windowSoftInputMode="adjustResize"
在您的登录 activity
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"/>
添加你的Scrollview
:
android:fillViewport="true"
并添加到您的 Manifest
文件中:
android:windowSoftInputMode="adjustResize"
我给你们看下面布局的正常视图:
Image 1
当我点击email editText时弹出软键盘,编辑文本被软键盘覆盖,如下图所示:
Image 2
所以,我想点击 editText,scrollView 会自动滚动到底部,而不是被软键盘覆盖,如下图所示:
Image 3
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>
试试这个用途让你的 ScrollView
android:fillViewport="true"
Defines whether the scrollview should stretch its content to fill the viewport.
并在您的清单文件中添加 android:windowSoftInputMode="adjustResize"
在您的登录 activity
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"/>
添加你的Scrollview
:
android:fillViewport="true"
并添加到您的 Manifest
文件中:
android:windowSoftInputMode="adjustResize"