首页 > 解决方案 > 键盘打开后更改 EditText 焦点

问题描述

当我点击它时,我注意到了 EditText 焦点的问题。通常,我有带有 RecyclerView 的 NestedScroll,其中有一些 EditText 控件。在点击其中一个后显示键盘,但之后我的焦点设置为从顶部开始的第一个 EditTest,而不是我点击的那个。

你有什么建议我该如何解决这个问题?

下面我放了我的活动布局的例子。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="@dimen/general_padding"
android:background="@color/window_color">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

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

            <!-- Title -->
            <LinearLayout
                android:id="@+id/titleContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/item_formt_element_title" />
                <include layout="@layout/item_formt_element_single_separator" />

            </LinearLayout>

            <!-- Tags -->
            <LinearLayout
                android:id="@+id/tagsContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/item_formt_element_tags" />
                <include layout="@layout/item_formt_element_single_separator" />

            </LinearLayout>

        </LinearLayout>

        <!-- Form elements -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/formRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            app:layout_constraintTop_toBottomOf="@id/genericSection"/>

        <!-- Footer -->
        <View
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@drawable/rounded_bottom"
            android:layout_marginBottom="@dimen/general_padding_bottom"
            app:layout_constraintStart_toStartOf="@id/formtsRecyclerView"
            app:layout_constraintEnd_toEndOf="@id/formtsRecyclerView"
            app:layout_constraintTop_toBottomOf="@id/formtsRecyclerView"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

标签: androidkotlinandroid-recyclerviewandroid-edittextandroid-textinputedittext

解决方案


你还没有共享代码。您是否尝试过在 onClick 方法中使用请求焦点?

public void onClick(View v) {
EditText ed = v.findViewById(R.id.yourEditText);
ed.requestFocus();
}

推荐阅读