Android XML - 在EditText字段之间移动

6
我有两个包含一些编辑框的片段,它们的布局相似,但是很奇怪的是其中一个可以使用Tab键在编辑框间切换,而另一个却不能。以下是我遇到问题的那个片段:
<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>

当我从第一个EditText框中切换时,焦点消失,然后在第二个选项卡上它将焦点移动到第二个字段。如果我再次切换,则焦点消失,然后再次切换,它会出现在其下方的按钮上。

在片段的onCreateView中没有什么特别的事情,除了获取指向EditText字段的指针。像我说的那样,我有一个非常相似的第二个视图,工作正常。

当我按Tab键时,我注意到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 }

编辑 -----------------

好的,根据评论建议,我为所有视图添加了onFocusChanged监听器(循环遍历并添加监听器),并记录了视图是什么。每次我按Tab键时,都会得到日志,但奇怪的是,我按Tab键到第一个EditText框时,日志显示该editText框具有焦点,再次按Tab键焦点丢失,但日志仍然显示相同的EditText框具有焦点!再次按Tab键,第二个得到焦点,日志显示正确的id,再次按Tab键焦点丢失,但日志仍然显示第二个EditText框具有焦点,即使它没有!!

非常奇怪的行为。


很抱歉,EditText无论如何都是可聚焦的。 - Darren
它可能与Fragment添加的方式有关吗?此外,当ExitText框中的内容消失时,可能有一种方法可以找出正在获取焦点的内容吗? - Darren
每次按Tab键以查找窃取焦点的人时,请调用此函数:https://dev59.com/xm435IYBdhLWcg3wigux#5352258 - TomTasche
请参见问题编辑。我添加了onFocusChanged监听器并记录了结果。 - Darren
1
我刚开始一个新项目,只添加了这个布局,它运行良好,这让我相信这是由于另一个片段覆盖了该片段而导致的问题? - Darren
显示剩余2条评论
2个回答

5

正如@Delyan所建议的,您应该能够添加一个XML属性来控制这种行为。不过,您可能想尝试使用其他android:nextFocus____属性之一,例如 android:nextFocusForward 。这些属性的完整列表在此处和以下位置可用。


android:nextFocusForward 可以正常工作 :-) 非常感谢。我之前不知道这个属性,但它似乎可以指定下一个要显示的视图,并且它有效。谢谢。 - Darren
另外,如果这回答了你的问题,请不要忘记接受 :) - Phil

1
您可以使用android:nextFocusDown来覆盖选项卡顺序。通常,Tab键向下,Shift+Tab键向上,方向键/轨迹球触发左/右聚焦查找。

谢谢。我已经尝试添加 android:nextFocusDown="@+id/passwordLog",但是我得到了需要按两次Tab键的相同行为。 - Darren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接