java - 如何检测用户何时完成向 RecyclerView 中的 EditText 输入值?

标签 java android kotlin android-recyclerview

enter image description here

我有一个包含 EditTexts 的 recyclerview。只有在用户完成在任何编辑文本中输入金额后,我如何才能获得每个编辑文本的值,以便我可以更新总金额。

我想要实现的是添加编辑文本的值并将其发送到 Activity 。在 Activity 中,我有 Proceed 按钮(我将在其中对总量执行一些验证)和总量 TextView(使用监听器从 recyclerview 适配器检索)。

我尝试使用 setOnEditorActionListener 但如果用户单击后退按钮而不是按回车键,这将无济于事。

此外,我尝试使用焦点更改监听器,但问题是即使在页面外部单击,EditText 也永远不会失去焦点。

当然,TextWatcher 不是一个理想的解决方案,因为它在 OnBindViewHolder 中可能非常昂贵。

我需要确保每当用户点击“继续”按钮时,总金额都会在之前更新。

最佳答案

想法

最好的方法是添加两个通信(通过接口(interface)):

  • ActivityAdapter 之间的第一个

  • SECOND 在 Adapter 和单个 ViewHolder 之间

添加此类通信后,您可以“实时”计算总和。

解决方案

步骤#1

创建第一个接口(interface),例如:

interface AdapterContentChanged {
    fun valuesChanged()
}

并在您的 Activity 中实现它或创建新变量(作为匿名类)。

第二步

在创建适配器时传递您的 Activity (或上述接口(interface)的实例),例如:

private val ownAdapter = OwnAdapter(
    items,    // elements inside list
    this      // interface implementation
)

第三步

创建第二个接口(interface),例如:

interface OwnViewHolderTextChanged {
    fun onTextChanged(position: Int, newValue: Int)
}

并在您的适配器中实现它或创建新变量(匿名类)- 与步骤 #1 相同。

第四步

在绑定(bind) viewHolder 时传递您的适配器(或变量)和位置(项目的),例如:

override fun onBindViewHolder(holder: OwnViewHolder, position: Int) {
    val number = list[position]
    holder.bind(number, position, this)
}

第 5 步

bind() 方法中(来自上面的示例),将新的 TextWatcher 添加到您的 EditText

afterTextChanged() 方法中(来自 TextWatcher)从接口(interface)调用方法并传递新值。例如:

fun bind(
    // TODO - add here more information which you need,
    position: Int,
    listener: OwnViewHolderTextChanged
) {
    itemView.edit_text.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            // Get EditText content
            val newValue = getNumber()

            // Call method from interface
            listener.onTextChanged(position = position, newValue = newValue)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            // Not used
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            // Not used
        }

    })
}

要从 EditText 计算值,您可以使用如下内容:

private fun getNumber(): Int =
    try {
        itemView.edit_text.text.toString().toInt()
    } catch (e: Exception) {
        0
    }

第六步

当在方法中更改文本时,您必须:

  • 更新列表内容

  • 通知适配器“有些东西被改变了”

例如:

override fun onTextChanged(position: Int, newValue: Int) {
    list[position] = newValue
    listener.valuesChanged()
}

第七步

当发生变化时(Activty 会知道),您可以计算新的总和:

override fun valuesChanged() {
    val sum: Int = ownAdapter.getCurrentSum()
    text_view.text = "Sum:  $sum"
}

演示

demo

关于java - 如何检测用户何时完成向 RecyclerView 中的 EditText 输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61828971/

相关文章:

java - 从 xmlGregorianCalender 到具有特定格式的日期

java - RemoteWebDriver IE 单击链接不起作用

android - 我的 BottomAppBar 覆盖了我的 RecyclerView 的最后一行

android - findViewById 仍然返回 null(新手)

android - 即使已在 ActionBarDrawerToggle 中设置了 Icon,也无法更改 DrawerLayout Icon

arrays - 如何在 Kotlin 中将对象转换为字节数组

java - Unix下执行Maven命令修改文件内容

java - 如何从 netbeans 将 int 值添加到 sql 列

android - 找不到类,使用 Android Studio 3.0.1、Room、Kotlin 的 androidTest 中的空测试套件

android - Kotlin plus() 不改变原始列表