java - 在 ScrollView 中,EditText 获得了不必要的焦点

标签 java android

我有一个用于 fragment 的布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/ll_root_view"
    app:theme="@style/SpennTheme.Light.NoActionBar">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/createBusinessTb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_collapseMode="pin"
        app:navigationIcon="@drawable/ic_close_screen">

    </androidx.appcompat.widget.Toolbar>



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">


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

            <RelativeLayout
                android:id="@+id/profilePicHolder"
                android:layout_width="match_parent"
                android:layout_height="216dp"
                android:background="@color/colorPrimaryDark">

                <ImageView
                    android:id="@+id/storePicIv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:visibility="visible"
                    app:srcCompat="@drawable/ic_store_image" />

                <ImageView
                    android:id="@+id/upload_pic_iv"
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentBottom="true"
                    android:layout_margin="@dimen/fab_margin"
                    android:layout_marginBottom="@dimen/fab_margin"
                    android:background="@drawable/shape_accent_circle"
                    android:clickable="true"
                    android:padding="14dp"
                    app:srcCompat="@drawable/ic_fab_edit" />
            </RelativeLayout>

            <View
                android:id="@+id/editbusiness_focus_thief"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:focusable="true"
                android:focusableInTouchMode="true">

                <requestFocus />
            </View>


            <TextView
                android:id="@+id/createBusinessHeadingTv"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginBottom="12dp"
                android:background="@color/pale_grey_three"
                android:gravity="center_vertical"
                android:paddingStart="@dimen/activity_margin"
                android:paddingEnd="@dimen/activity_margin"
                android:text="Information"
                android:textColor="@color/gunmetal" />

        <FrameLayout
            android:id="@+id/textInputContent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/nameTil"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="0dp"
                android:hint="Name on map"
                app:hintEnabled="true">

                <EditText
                    android:id="@+id/nameEt"
                    style="@style/EditTextProfile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences"
                    android:lines="1" />
            </com.google.android.material.textfield.TextInputLayout>
        </FrameLayout>


        <FrameLayout
            android:id="@+id/textInputContent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/nameTil"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="0dp"
                android:hint="Name on map"
                app:hintEnabled="true">

                <EditText
                    android:id="@+id/addressEt"
                    style="@style/EditTextProfile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionDone"
                    android:inputType="textCapSentences"
                    android:lines="1" />
            </com.google.android.material.textfield.TextInputLayout>
        </FrameLayout>


            <View
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:visibility="invisible" />

            <Button
                style="@style/ButtonStyleInactive"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="4dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="4dp"
                android:enabled="true"
                android:text="Create" />
            <View
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:visibility="invisible" />
        </LinearLayout>

    </ScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我在键盘的完成按钮后清除焦点,如下所示:

et1.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    NKeyboardController.hideKeyboard(getActivity());
                    et1.getTextInput().clearFocus();
                    et1.getTextInput().setFocusableInTouchMode(true);
                    return true;
                }
                return false;
            });

et2.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    NKeyboardController.hideKeyboard(getActivity());
                    et2.getTextInput().clearFocus();
                    et2.getTextInput().setFocusableInTouchMode(true);
                    return true;
                }
                return false;
            });

但是,在单击键盘的完成按钮后,第二个总是聚焦于第一个编辑文本。

我尝试在键盘操作中像这样从父 View 中删除焦点:

rootView.clearFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

但是这里没有任何东西可以帮助我。这里需要建议。

最佳答案

您可以在主布局上使用以下代码来禁用编辑文本焦点

<AutoCompleteTextView 
    android:id="@+id/autotext"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:nextFocusLeft="@id/autotext"
    android:nextFocusUp="@id/autotext" />

关于java - 在 ScrollView 中,EditText 获得了不必要的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57923058/

相关文章:

android - 如何动态改变形状颜色?

java - 在 Java 中删除二维数组中的空元素

android - 是否需要调用 PreferenceManager.setDefaultValues() 来清除首选项?

java - Play Framework 2.5 应用程序在没有明显原因的情况下崩溃

java - 使用 java 创建 YouTube 事件而不提示从浏览器登录

Android 在 addTextChangedListener (edittext) 的某些情况下禁用正面按钮

Android OpenCV VideoCapture::retrieve(&mat) 导致致命信号 11

java - 如何在数据库触发器创建 id 时禁用生成器

javax.net.ssl.SSLHandshakeException?

java - Spring boot - 没有嵌入式tomcat的Rest Call客户端