Android XML - moving between EditText fields

2020-03-03 06:19发布

问题:

I've got 2 Fragments that have a couple of EditText boxes on. They are both laid out similar, however its very strange that 1 will tab between the EditText fields, but the other won't.

This is the one i'm having problems with:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/loginparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff008DFF" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dip"
        android:background="#ff008DFF"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="40dip"
            android:contentDescription="@string/logoDesc"
            android:src="@drawable/logo1" />

        <EditText
            android:id="@+id/usernameLog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:hint="@string/username"
            android:imeOptions="actionNext"
            android:singleLine="true"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/passwordLog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:imeOptions="actionDone"
            android:inputType="textPassword"
            android:singleLine="true"
            android:textSize="14sp" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/login" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="@string/newuser"
                android:textColor="#ffffffff"
                android:textSize="14sp" />

            <Button
                android:id="@+id/viewSignupBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/signup" />
        </LinearLayout>
    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBarLog"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:padding="70dip"
        android:visibility="gone" />

</RelativeLayout>

When I tab from the first EditText box, the focus disappears then on a 2nd tab it moves focus to the 2nd field. Same again if I tab focus goes, then tab again and it appears on the button below it.

There's nothing special going on in the Fragment's onCreateView apart from getting pointers to the EditText fields. Like I say, I have a 2nd view very similar that works fine.

When I tab, I notice I get these warnings showing up in Logcat:

04-24 19:31:29.695: D/InputEventConsistencyVerifier(2040): KeyEvent: ACTION_UP but key was not down.
04-24 19:31:29.695: D/InputEventConsistencyVerifier(2040):   in android.support.v4.app.NoSaveStateFrameLayout@40f365b8
04-24 19:31:29.695: D/InputEventConsistencyVerifier(2040):   0: sent at 4325570000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=4325570, downTime=4325514, deviceId=0, source=0x101 }
04-24 19:31:31.076: D/InputEventConsistencyVerifier(2040): KeyEvent: ACTION_UP but key was not down.
04-24 19:31:31.076: D/InputEventConsistencyVerifier(2040):   in android.widget.EditText@40f78e98
04-24 19:31:31.076: D/InputEventConsistencyVerifier(2040):   0: sent at 4326945000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=4326945, downTime=4326889, deviceId=0, source=0x101 }
04-24 19:31:32.255: D/InputEventConsistencyVerifier(2040): KeyEvent: ACTION_UP but key was not down.
04-24 19:31:32.255: D/InputEventConsistencyVerifier(2040):   in android.widget.EditText@40f73678
04-24 19:31:32.255: D/InputEventConsistencyVerifier(2040):   0: sent at 4328123000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=4328123, downTime=4328053, deviceId=0, source=0x101 }

EDIT -----------------

Ok, as suggested in comments, I added onFocusChanged listeners to all views (Looped through them all and added listener) and Logged what the view is. Each time I tab, I get the log but the strange thing is, I tab to the first EditText box and the Log says that editText box has focus, I tab again and focus is lost but the log says the same EditText box has focus! Tab again and the second gets focus and log says the correct id, tab again focus lost but again log says the 2nd EditText box has focus even though it hasn't!!

Very strange behaviour.

回答1:

As @Delyan suggests, you should be able to add an XML attribute to control this behavior. However, you may want to try using one of the other android:nextFocus____ attributes, such as android:nextFocusForward. A full list of these attributes is available here and below.



回答2:

You can use android:nextFocusDown to override the tab order. In general, tab looks down, shift+tab looks up and the directional keys/trackball trigger the left/right focus lookup.