错误的 EditText 在 ListView 中获得焦点

Wrong EditText get focus in ListView

我在 ListView 中有 EditText。当焦点位于第一项时单击下一步后,焦点丢失,下一步按钮变为换行按钮。在其他项目上一切正常。

我的列表行布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/field_row_height">

    <EditText
        android:id="@+id/field"
        style="@style/EditText.Form.Field"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/double_default_padding"/>

    <Button
        android:id="@+id/required"
        style="@style/Button.Gray"
        android:layout_width="@dimen/required_button_width"
        android:layout_height="@dimen/required_button_height"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/default_padding"
        android:gravity="center"
        android:padding="0dp"
        android:text="@string/form_field_required"
        android:textColor="@color/white"/>

试试这个

<EditText
        android:id="@+id/field"
        style="@style/EditText.Form.Field"
        android:layout_width="match_parent"
        android:focusable="true"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/double_default_padding"/>

你忘了这个

android:focusable="true"
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/field_row_height">

        <EditText
            android:id="@+id/field"
            style="@style/EditText.Form.Field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="@dimen/double_default_padding">
    <requestFocus />
    </EditText>
        <Button
            android:id="@+id/required"
            style="@style/Button.Gray"
            android:layout_width="@dimen/required_button_width"
            android:layout_height="@dimen/required_button_height"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/default_padding"
            android:gravity="center"
            android:padding="0dp"
            android:text="@string/form_field_required"
            android:textColor="@color/white"/>




listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); android:windowSoftInputMode="adjustPan"          in your activity or android:descendantFocusability="beforeDescendants"

如果您在视图打开时放置请求焦点,请求的小部件将获得焦点。

<EditText
            android:id="@+id/field"
            style="@style/EditText.Form.Field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="@dimen/double_default_padding">
    <requestFocus />
    </EditText>

解决方案:

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);