android - 从另一个 fragment 导航回来时自定义 View 的奇怪行为

标签 android kotlin

在将自定义 View 与导航组件和后退按钮一起使用时,我遇到了奇怪的行为。

对于用户文本输入,我使用 Material Design 文本字段(它是 TextInputLayout 和 TextInputEditText 的组合)。为了避免代码重复,我创建了一个自定义 View - CustomTextField(遵循 this 指南),它将这两个 View 合并为一个 View 。

我还使用导航组件在单个 Activity 中的 Fragment 之间导航。

问题 - 在我的 LoginFragment 中,我使用该自定义 View 的 2 个实例来显示用户输入的字段。当我在这些文本字段中输入 2 个不同的值,导航到另一个 fragment (使用“注册”按钮 - 现在它只是一个空白 fragment ),然后使用“后退”按钮(或手势)返回到 登录 fragment 第一个文本字段的值(文本)成为第二个文本字段的副本

例如:(屏幕截图作为链接上传,因为这是我在 StackOverflow 上提出的第一个问题,我不允许将它们直接插入到问题中)

LoginFragment with 2 different values provided by user

LoginFragment after navigating to another fragment and going back using the button/gesture - first field is now a duplicate of "pass"

为了解决这个问题,我已经将代码减少到最少,但问题仍然发生。 这是我正在使用的代码:

自定义文本字段 xml (custom_text_field.xml):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/customTextLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/customEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>
</merge>

自定义文本字段类:

class CustomTextField(context: Context, attrs: AttributeSet): LinearLayout(context, attrs) {

    init {
        inflate(context, R.layout.custom_text_field, this)
    }
}

LoginFragment xml 中 CustomTextField 的用法:

        <com.example.app.views.CustomTextField
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <com.example.app.views.CustomTextField
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

导航到另一个 fragment 是使用以下方法完成的:

        binding = FragmentLoginBinding.inflate(layoutInflater)

        binding.buttonRegister.setOnClickListener {
            findNavController().navigate(R.id.action_loginFragment_to_registerFragment)
        }

我还注意到,不仅文本是“重复的”,而且还有一些其他属性,例如提示。某些属性(例如 inputType)按预期工作并且不会重复。

如果您能帮助解决该问题,我将不胜感激。预先感谢您!

最佳答案

这与系统恢复 View 状态的方式有关。默认情况下,ID 用于唯一标识用于状态恢复的 View ,因此 TextInputEditText 具有重复的 ID 可以解释镜像状态。

关于android - 从另一个 fragment 导航回来时自定义 View 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67315082/

相关文章:

android - 通过我的网站向特定的安卓设备发送通知

java - 使用 android studio 构建 Selenium 项目失败

android - constructor() 是 Kotlin 中的主要构造函数吗?

eclipse - 如何让IntelliJ IDEA高亮显示接口(interface)已实现的方法

javafx - Tornadofx tableview 使用组合框并在按钮上添加 FXEvent

android - 在 Kotlin 中使用新参数的分页 3.0 列表

android - 掉落时的 RecyclerView

java - 有没有办法验证apk文件是否是从给定的源代码生成的?

Android Sync 适配器一直运行

java - Android Studio : getStandardDeviation() Method from org. apache.commons.math4.stat.descriptive 没有给我 Std 的确切值