到目前为止,我已经完成了:
MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val email: EditText = this.findViewById(R.id.edt_mail)
val pass: EditText = this.findViewById(R.id.edt_password)
val scrollView = findViewById<View>(R.id.nestedScrollView) as ScrollView
email.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
scrollView.postDelayed({
scrollView.fullScroll(View.FOCUS_DOWN)
}, 1200)
}
}
pass.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
scrollView.postDelayed({
scrollView.fullScroll(View.FOCUS_DOWN)
}, 1200)
}
}}}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logo"
android:layout_width="258dp"
android:layout_height="47dp"
android:layout_marginTop="164dp"
android:contentDescription="@string/companyLogo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/LoginGuideLine"
style="@style/LoginGuideLine"
android:layout_width="295dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:padding="20dp"
android:text="@string/LoginGuideLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />
<android.support.design.widget.TextInputLayout
android:id="@+id/edtl_login"
style="@style/TextInput"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LoginGuideLine">
<android.support.design.widget.TextInputEditText
android:id="@+id/edt_mail"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:focusable="true"
android:hint="@string/EmailHint"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/edtl_password"
style="@style/TextInput"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtl_login">
<android.support.design.widget.TextInputEditText
android:id="@+id/edt_password"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:focusable="true"
android:hint="@string/PasswordHint"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/LoginBtn"
style="@style/PrimaryBtn"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="@string/BtnTxt"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtl_password" />
</android.support.constraint.ConstraintLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="constraints">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
这基本上是我正在制作的登录页面UI,我希望屏幕在关注电子邮件或密码字段时自动向下滚动。在电子邮件领域,它完成工作,但在密码字段,它将焦点自动切换回电子邮件字段,我试图重置焦点,但似乎没有工作,所以我不知道问题出在哪里