我想知道是否有其他人遇到过这个奇怪的错误。我的布局很复杂,但本质上是带有 EditText 字段的 View 持有者行。
在我将目标 SDK 更新到 API 30 之前,它运行良好。
以 API 30 为目标后,我的一些 EditText 字段不响应“焦点触摸事件”(光标句柄不显示,不能长按,也不能选择任何文本)。 您仍然可以在字段中键入并通过点击移动光标。同样,一些文本字段工作得很好,而另一些则受到此错误的影响。
^ 图像不是我的应用程序,只是为了显示光标处理程序以供理解。
约束布局和线性布局都有相同的错误。
<!-- 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.0
和 androidx.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 并更新您的约束。请在下面找到屏幕截图。这些是我所做的更改。
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>
关于Android 的某些 EditText 字段不响应长按(API 30+ with layout_width 0dp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70025541/