Android 某些 EditText 字段不响应长按(API 30+,layout_width 0dp)

Lif*_*fes 5 android android-appcompat android-edittext android-wrap-content android-api-30

我想知道是否还有其他人经历过这个奇怪的错误。我的布局很复杂,但本质上它是带有 EditText 字段的成排视图持有者。

在我将目标 SDK 更新到 API 30 之前,它一直完美运行。

针对 API 30 后,我的一些EditText 字段不响应“焦点触摸事件”(光标手柄不显示,无法长按,也无法选择任何文本)。您仍然可以在字段中输入内容并通过点击移动光标。同样,某些文本字段工作得很好,而其他文本字段则受到此错误的影响。

在此输入图像描述

^ 该图像不是我的应用程序,只是为了显示光标处理程序以供理解。

约束布局和线性布局都有同样的bug。

<!-- in constraint layout -->
<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/myTextField"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

<!-- in linear layout -->
<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/myTextField"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)

如果我将文本字段宽度更改为wrap_content它,它对于所有文本字段都适用,但这不适合我的设计。我需要文本字段来填充可用空间。

我什至尝试手动将宽度设置为随机 dp 值,但它仍然不起作用。

我已经在 < API 30 设备上进行了测试,一切都很好。

我也有用户在生产中报告过这个问题。

我错过了什么?为什么这不适用于 API 30+?

还有另一种方法可以让 EditText 字段填充空间吗?

更新(仍未解决):

我已将 Android Studio 更新到最新版本Arctic Fox 2020.3.1.。我还将 gradle 更新为7.0.3,将 kotlin 更新为1.5.31,并将我的所有库更新为最新版本。我正在使用androidx.core:core-ktx:1.7.0androidx.constraintlayout:constraintlayout:2.1.2

我进一步调查并发现了更多的奇怪之处。以编程方式设置任何文本或任何提示都会导致该错误。在 xml 中设置文本或提示,完全没问题,没有错误。文本值来自数据库,因此我需要能够以编程方式设置它们。

// setting hint or text programmatically in .kt file causes the bug
// any one of the lines below will cause the bug

myTextField.setHint("myTextField hint")
myTextField.hint = "myTextField hint"
myTextField.setText("myTextField text")
myTextField.text = "myTextField text"

// setting hint or text in .xml file does not cause the bug

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/myTextField"
    ...
    android:hint="myTextField hint"
    android:text="myTextField text"
    ... />
Run Code Online (Sandbox Code Playgroud)

更新2(仍未解决):

我向 Google 提出了问题,但他们尚未解决该问题。

https://issuetracker.google.com/issues/209938888

Dan*_*aku 1

我试图复制你的问题。它可以与 targetSdk 30 配合使用,并更新您的约束。请在下面找到屏幕截图。这些是我所做的改变。

  1. 我更新了您正在使用的约束属性,因为它们已经过时了。例如。我将“layout_constraintLeft_toRightOf”更改为“layout_constraintStart_toStartOf”。
  2. 您声明约束属性的方式将视图置于父级的可见空间之外。例如。'app:layout_constraintLeft_toRightOf="parent"' 将视图的左侧放置到父级的末尾,这使得视图的大部分不可见。

我根据您在问题中发送的代码更新了您设置约束的方式。在下面找到更新的代码。您还可以查看下面的屏幕截图,看看它是否有效。

        <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        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"
        tools:context=".Activities.MainActivity">

        <!-- THIS IS THE CONSTRAINT LAYOUT-->

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/the_constraintlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/the_lineartlayout">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/myTextField"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:text="ConstraintLayout Sample Text"
                android:gravity="center"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

            <!--
            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/myTextField"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:text="This is a sample text"
                android:gravity="center"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintLeft_toRightOf="parent"
                app:layout_constraintRight_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
                -->
        </androidx.constraintlayout.widget.ConstraintLayout>

        <!-- THIS IS THE LINEAR LAYOUT-->
        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/the_lineartlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/the_constraintlayout"
            app:layout_constraintBottom_toBottomOf="parent">
            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/myTextField2"
                android:layout_height="wrap_content"
                android:text="LinearLayoutCompat Sample Text"
                android:layout_width="0dp"
                android:layout_weight="1"/>
        </androidx.appcompat.widget.LinearLayoutCompat>
    </androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述 在此输入图像描述

  • 它是一个 RecyclerView,其中每个视图持有者都有自己的 EditText 视图。每次只会发生在几个视图持有者身上(这是随机的)。我将尝试您的更改,看看它们是否有效。 (2认同)