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

标签 android android-edittext android-appcompat android-wrap-content android-api-30

我想知道是否有其他人遇到过这个奇怪的错误。我的布局很复杂,但本质上是带有 EditText 字段的 View 持有者行。
在我将目标 SDK 更新到 API 30 之前,它运行良好。
以 API 30 为目标后,我的一些 EditText 字段不响应“焦点触摸事件”(光标句柄不显示,不能长按,也不能选择任何文本)。 您仍然可以在字段中键入并通过点击移动光标。同样,一些文本字段工作得很好,而另一些则受到此错误的影响。
enter image description here
^ 图像不是我的应用程序,只是为了显示光标处理程序以供理解。
约束布局和线性布局都有相同的错误。

<!-- 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"/>
如果我将文本字段宽度更改为 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"
    ... />

最佳答案

我试图复制你的问题。它适用于 targetSdk 30 并更新您的约束。请在下面找到屏幕截图。这些是我所做的更改。

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

  • I updated how you set your constraints based the code you sent in your question. Find the updated code below. You can also look at the screenshots below to see that it works.

            <?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>
    
    enter image description here
    enter image description here
    enter image description here

    关于Android 的某些 EditText 字段不响应长按(API 30+ with layout_width 0dp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70025541/

    相关文章:

    安卓 : How to disable Keyboard or making enable only the numeric in EditText onClick

    android - 设置 SupportActionBar 字幕多行

    android - 何时使用 AppCompatView 与普通 Android View

    Android:无法输入 EditText

    Android 关闭键盘 "naturally"

    android - 为设备所有者应用程序安装更新

    android - 如何为您的 Adapter 选择最佳构造函数?

    Android - API 23 不调用 onAttach(Context)

    android - 如何在同一 Activity 中为多个编辑文本设置不同的文本选择相关颜色?

    android - 想 toast 的用户名,但是当我尝试登录时却给了我很大的压力