编辑文本不会成为焦点

时间:2015-06-23 17:23:14

标签: android android-edittext

我有一个包含多个编辑文本的视图。当我直接为视图充气以进行测试时,它可以工作。编辑文本聚焦并允许输入,但是一旦我尝试将其添加到列表视图中,编辑文本就不会聚焦。我尝试添加属性

  

机器人:descendantFocusability =" afterDescendants"

仍然没有工作,所以我现在通过从我的适配器充气来手动将视图添加到线性布局,我仍然遇到同样的问题。有什么帮助吗?

这是观点。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <TextView
            android:id="@+id/question_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/alarm_name"
            tools:text="1" />

        <View
            android:id="@+id/num_divider"
            android:layout_width="1dp"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/question_number"
            android:layout_marginBottom="10dp"
            android:layout_toEndOf="@+id/question_number"
            android:layout_toRightOf="@+id/question_number"
            android:background="@color/alarm_text" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/comment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:layout_toEndOf="@+id/num_divider"
            android:layout_toRightOf="@+id/num_divider"
            android:focusable="true"

            android:hint="Enter question here."
            app:met_baseColor="@color/alarm_name"
            app:met_clearButton="true"
            app:met_errorColor="@android:color/holo_red_dark"
            app:met_hideUnderline="true"
            app:met_maxCharacters="160"
            app:met_primaryColor="@color/colorPrimary"

            app:met_textColor="@color/colorPrimary"
            app:met_textColorHint="@color/alarm_name"
            app:met_underlineColor="@color/alarm_name"

            />
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/alarm_name" />

    <LinearLayout
        android:id="@+id/choices"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/choice_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="A"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@color/alarm_name" />

            <View
                android:id="@+id/num_divider2"
                android:layout_width="1dp"
                android:layout_height="20dp"
                android:layout_alignBottom="@+id/choice_one"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/choice_one"
                android:layout_toRightOf="@+id/choice_one"
                android:background="@color/alarm_text" />

            <com.rengwuxian.materialedittext.MaterialEditText
                android:id="@+id/choice_one_input"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:layout_toEndOf="@id/num_divider2"
                android:layout_toRightOf="@+id/num_divider2"

                android:hint="Enter choice here."
                app:met_baseColor="@color/alarm_name"
                app:met_clearButton="true"
                app:met_errorColor="@android:color/holo_red_dark"
                app:met_maxCharacters="160"
                app:met_primaryColor="@color/colorPrimary"

                app:met_textColor="@color/colorPrimary"
                app:met_textColorHint="@color/alarm_name"
                app:met_underlineColor="@color/alarm_name" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/choice_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="B"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@color/alarm_name" />

            <View
                android:id="@+id/num_divider3"
                android:layout_width="1dp"
                android:layout_height="20dp"
                android:layout_alignBottom="@+id/choice_two"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/choice_two"
                android:layout_toRightOf="@+id/choice_two"
                android:background="@color/alarm_text" />

            <com.rengwuxian.materialedittext.MaterialEditText
                android:id="@+id/choice_two_input"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:layout_toEndOf="@id/num_divider3"
                android:layout_toRightOf="@+id/num_divider3"

                android:hint="Enter choice here."
                app:met_baseColor="@color/alarm_name"
                app:met_clearButton="true"
                app:met_errorColor="@android:color/holo_red_dark"
                app:met_maxCharacters="160"
                app:met_primaryColor="@color/colorPrimary"

                app:met_textColor="@color/colorPrimary"
                app:met_textColorHint="@color/alarm_name"
                app:met_underlineColor="@color/alarm_name" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/choice_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="C"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@color/alarm_name" />

            <View
                android:id="@+id/num_divider4"
                android:layout_width="1dp"
                android:layout_height="20dp"
                android:layout_alignBottom="@+id/choice_three"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/choice_three"
                android:layout_toRightOf="@+id/choice_three"
                android:background="@color/alarm_text" />

            <com.rengwuxian.materialedittext.MaterialEditText
                android:id="@+id/choice_three_input"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:layout_toEndOf="@id/num_divider4"
                android:layout_toRightOf="@+id/num_divider4"

                android:hint="Enter choice here."
                app:met_baseColor="@color/alarm_name"
                app:met_clearButton="true"
                app:met_errorColor="@android:color/holo_red_dark"
                app:met_maxCharacters="160"
                app:met_primaryColor="@color/colorPrimary"

                app:met_textColor="@color/colorPrimary"
                app:met_textColorHint="@color/alarm_name"
                app:met_underlineColor="@color/alarm_name" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/choice_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="D"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@color/alarm_name" />

            <View
                android:id="@+id/num_divider5"
                android:layout_width="1dp"
                android:layout_height="20dp"
                android:layout_alignBottom="@+id/choice_four"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/choice_four"
                android:layout_toRightOf="@+id/choice_four"
                android:background="@color/alarm_text" />

            <com.rengwuxian.materialedittext.MaterialEditText
                android:id="@+id/choice_four_input"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:layout_toEndOf="@id/num_divider5"
                android:layout_toRightOf="@+id/num_divider5"

                android:hint="Enter choice here."
                app:met_baseColor="@color/alarm_name"
                app:met_clearButton="true"
                app:met_errorColor="@android:color/holo_red_dark"
                app:met_maxCharacters="160"
                app:met_primaryColor="@color/colorPrimary"

                app:met_textColor="@color/colorPrimary"
                app:met_textColorHint="@color/alarm_name"
                app:met_underlineColor="@color/alarm_name" />

        </RelativeLayout>

    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/alarm_name" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Spinner
            android:id="@+id/answer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:entries="@array/choices"></Spinner>

        <View
            android:id="@+id/answer_divider"
            android:layout_width="1dp"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/answer"
            android:layout_marginBottom="10dp"
            android:layout_toEndOf="@+id/answer"
            android:layout_toRightOf="@+id/answer"
            android:background="@color/alarm_text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/answer_divider"
            android:layout_toEndOf="@+id/answer_divider"
            android:layout_toRightOf="@+id/answer_divider"
            android:text="IS THE ANSWER" />
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/alarm_name" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <com.mikepenz.iconics.view.IconicsImageView
                android:id="@+id/view3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginRight="10dp"
                app:iiv_color="@color/colorPrimary"
                app:iiv_icon="cmd-help" />

            <Spinner
                android:id="@+id/question_type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:entries="@array/question_type" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <com.mikepenz.iconics.view.IconicsImageView
                android:id="@+id/delete_button"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginRight="10dp"
                app:iiv_color="@color/colorPrimary"
                app:iiv_icon="cmd-delete" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/delete_button"
                android:layout_toEndOf="@+id/delete_button"
                android:layout_toRightOf="@+id/delete_button"
                android:text="Delete" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

具有线性布局的主要活动。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wake.social.ui.QuestionsActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:elevation="4dp"/>
<ScrollView
    android:layout_below="@id/toolbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/questionsList"
        android:orientation="vertical"
        android:layout_height="fill_parent"/>
</ScrollView>

</RelativeLayout>

enter image description here

1 个答案:

答案 0 :(得分:1)

我必须在适配器中充气后手动设置所有项目的焦点。

    holder.comment.setFocusableInTouchMode(true);
    holder.comment.requestFocus();

    holder.choiceOneInput.setFocusableInTouchMode(true);
    holder.choiceOneInput.requestFocus();

    holder.choiceTwoInput.setFocusableInTouchMode(true);
    holder.choiceTwoInput.requestFocus();

    holder.choiceThreeInput.setFocusableInTouchMode(true);
    holder.choiceThreeInput.requestFocus();

    holder.choiceFourInput.setFocusableInTouchMode(true);
    holder.choiceFourInput.requestFocus();