[当我触摸EditText
时,我希望scrollView
滚动一些px。问题是,仅当我第二次触摸相应的EditText并且焦点位于另一个editText上时,它才起作用。这是代码:
KOTLIN:
usernameLogin.setOnFocusChangeListener(object: View.OnFocusChangeListener {
override fun onFocusChange(view:View, hasFocus:Boolean) {
if (hasFocus){
scroll_login.scrollTo(0, 240);
}else{
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show()
}
}
})
XML
<ScrollView
android:id="@+id/scroll_login"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_usernameLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/usernameLogin"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="E-mail"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="Password"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
</ScrollView>
请确保您的EditText不在默认值上(例如,当您切换到此片段/活动时)。为避免这种情况,请尝试在该特定EditText上使用clearFocus()或在该特定EditText上使用requestFocus()其他的东西