Android-onFocusChange第一次不起作用

问题描述 投票:0回答:1

[当我触摸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>
android kotlin android-edittext focus onfocuschangelistener
1个回答
0
投票

请确保您的EditText不在默认值上(例如,当您切换到此片段/活动时)。为避免这种情况,请尝试在该特定EditText上使用clearFocus()或在该特定EditText上使用requestFocus()其他的东西

© www.soinside.com 2019 - 2024. All rights reserved.