TextInputEditText获焦自动滚动异常:密码框焦点跳回邮箱问题求助
Hey there! Let's figure out why your Password TextInputEditText is bouncing focus back to the Email field when it should trigger a screen scroll. This is a super common Android UI quirk, so let's walk through the most likely fixes step by step.
1. Double-Check Focusable Attributes in Your Layout
First, make sure your Password input field has the correct focus-related properties. Sometimes a missing attribute can cause the system to prioritize the Email field automatically.
Update your activity_main.xml for the Password input:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/passwordInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:focusable="true" android:focusableInTouchMode="true" /> </com.google.android.material.textfield.TextInputLayout>
Also, ensure you haven't accidentally set android:focusable="false" on any parent layout of the Password field—this would block it from receiving focus entirely.
2. Adjust Scroll Container's Focus Behavior
If your form is inside a ScrollView or NestedScrollView, the container's descendantFocusability property might be interfering with focus logic. This property controls how child views gain focus relative to the parent.
Set your scroll container to prioritize child views:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="afterDescendants"> <!-- Your full login form layout here --> </ScrollView>
afterDescendants tells the system to let child views (like your input fields) receive focus before the scroll container itself, which should prevent unwanted focus jumps.
3. Audit Focus-Related Code in MainActivity
Check if you have any OnFocusChangeListener or focus-request logic that's accidentally forcing focus back to the Email field. For example, if you added code to scroll when the Email field gains focus, make sure it doesn't leak into the Password field's handling.
Here's a clean example of how to handle scroll on focus without cross-contamination:
val scrollView = findViewById<ScrollView>(R.id.loginScrollView) val emailEditText = findViewById<TextInputEditText>(R.id.emailEditText) val passwordEditText = findViewById<TextInputEditText>(R.id.passwordEditText) emailEditText.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) { scrollView.smoothScrollTo(0, emailEditText.bottom) } } passwordEditText.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) { scrollView.smoothScrollTo(0, passwordEditText.bottom) // Make sure there's NO line like emailEditText.requestFocus() here! } }
Also, look for any emailEditText.requestFocus() calls in onCreate() or onResume()—if you're auto-focusing the Email field on startup, this might trigger again when the layout adjusts for the soft keyboard, yanking focus back. Try removing that auto-focus line or adding a small delay if you still need it.
4. Tweak Window Soft Input Mode
The windowSoftInputMode in AndroidManifest.xml can affect how the layout adjusts when the soft keyboard pops up, which sometimes causes unexpected focus shifts.
Update your MainActivity entry to use a mode that plays nicely with scrolling:
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan|stateHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
adjustPan shifts the entire screen up to keep the focused field visible, while stateHidden prevents the keyboard from popping up automatically on launch (which can reduce layout jank).
Final Troubleshooting Tip
If none of the above works, try temporarily removing any custom touch listeners or input filters attached to either field—sometimes these can interfere with the system's default focus handling. Once you isolate the issue, you can reintroduce them with fixes.
内容的提问来源于stack exchange,提问作者TheNoobster

