【问题标题】:Kotlin - Problems to read text of dinamically edit textKotlin - 读取动态编辑文本的问题
【发布时间】:2019-03-02 19:34:06
【问题描述】:

我创建了一个动态编辑文本。第一行是静态的,并且集成在布局上。下一行是动态的,您的布局在另一个 xml 中。

我可以读取第一行(静态行)的值,但是当用户触摸键盘中的“下一个”选项时。我尝试使用“addTextChangedListener”,但文本看起来像“o;on;one”。

动态行我无法读取值。

这是我的代码:

/*Add Line*/
holder.add_field_button.setOnClickListener {
    holder.parent_linear_layout.apply {
        val inflater = LayoutInflater.from(context)
        val rowView = inflater.inflate(R.layout.used_products_field, this, false)
        val rowViewDeleteButton = rowView.delete_button_din
        val readTextDin = rowView.number_edit_text_din

        holder.parent_linear_layout.addView(rowView, holder.parent_linear_layout.childCount!!)

        readTextDin.setOnEditorActionListener { v, actionId, event ->
                        if (actionId == EditorInfo.IME_ACTION_DONE) {
                            if (rowView.number_edit_text_din.text.isNotEmpty()) {
                                intervObject.Products += rowView.number_edit_text_din.text.toString() + ";"
                            }
                        }
                        false
                    }

        rowView.delete_button_din.setOnClickListener {
            holder.parent_linear_layout.removeView(it.parent as View)
        }

        //remove row
        removeField(holder.delete_button, holder.parent_linear_layout)
    }
}

/*First Line*/
holder.number_edit_text.setOnEditorActionListener { v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    holder.number_edit_text.clearFocus()
                    hideKeyboardFrom(context, holder.number_edit_text)

                    if (holder.number_edit_text.text.isNotEmpty()) {
                        intervObject.Products += holder.number_edit_text.text.toString() + ";"
                    }
                }
                false
}

我该如何解决我的问题?

【问题讨论】:

    标签: android kotlin android-edittext


    【解决方案1】:

    你的问题来了

    intervObject.Products += holder.number_edit_text.text.toString() + ";"
    

    每次更改它时,都会将当前文本添加到此变量中。所以当它变为o时,你将添加o;,然后当它变为on时,你将添加on;

    您可以将其存储在地图中,使用 edittext 作为键。这样每次更改时,您只会替换值,而不是添加不同的版本!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2022-10-23
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多