我有一个卡片视图适配器,其中每张卡片都是填写必填值(如果用户想要保存该值)和可选注释框的表单。 此强制值和可选注释框是 TextInputLayouts 中的 TextInputEditText 字段。 在后面的代码中,我为每个字段都有 Textwatchers。值字段查看输入并验证该值。如果有效,则将启用注释框并将该值设置到对象上。评论框的文本观察器仅获取输入中的字符串并将其设置到同一对象上。 前任。
object.setValue(); object.setComment();
我在 UI 中是这样实现的:
<TextView android:id="@+id/txt_value_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Value:*" android:textAppearance="@style/Bold.FontPath" android:textSize="@dimen/cardview_textsize_header" android:textStyle="bold" />
`<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_edt_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edt_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/twenty_dp"
android:background="@drawable/rounded_border"
android:inputType="text"
android:lines="1"
android:maxLines="1"
android:textColor="@color/black"
android:textSize="@dimen/cardview_textsize_header" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/txt_commentbox_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/ten_dp"
android:text="Title"
android:textAppearance="@style/Bold.FontPath"
android:textSize="@dimen/cardview_textsize_header"
android:textStyle="bold" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_edt_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="255"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edt_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:gravity="top"
android:background="@drawable/rounded_border"
android:inputType="textMultiLine|textCapSentences"
android:maxEms="255"
android:maxLength="255"
android:maxLines="3"/>
</com.google.android.material.textfield.TextInputLayout>`
问题在于,使用外部键盘输入值完全可以正常工作。但是,当我专注于评论框时,按一下键后,它会选择标题中的“后退”按钮。 它在 Android 12 之前的 Android 版本上运行完全正常。
我一直在分析后面代码中的事件,但是除了使用文本观察器将字符串值保存到对象上之外,编辑后似乎没有任何东西可以改变注释框。
但是我发现日志中只弹出commentbox-edittext:
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
这感觉像是问题所在,但我找不到解决方案。
有人知道吗?
我尝试过更改用户界面。我尝试过使用 EditTexts。我已尝试将
"com.google.android.material:material:1.3.0"
更新到最新版本。我还查看了整个代码隐藏的逻辑并完全删除了触发器,看看它是否会改变任何东西。我添加了一个新的干净的编辑文本,它可以工作,但我的评论框仍然没有按预期工作。
在用户界面中进行进一步测试后,我找到了一个“解决方法”。这并不完全是我所希望的,但它确实有效。
在我的 TextInputLayout 中,当 counterEnabled 为 true 时,它会自动取消选择文本更改时的 EditText。
> <com.google.android.material.textfield.TextInputLayout
> android:id="@+id/layout_edt_comment"
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> app:counterEnabled="true"
> app:counterMaxLength="255"
> app:endIconMode="clear_text"></com.google.android.material.textfield.TextInputLayout>
所以我删除了它,现在它可以工作了。有谁知道这个问题的真正解决方案吗?我确实需要一个文本计数器。我还可以添加一个文本查看器并在 TextWatcher 中更新它。但这仍然感觉像是一种解决方法。