【问题标题】:Kotlin Get the value of an EditTextKotlin 获取一个 EditText 的值
【发布时间】:2019-10-10 11:25:58
【问题描述】:

我正在使用 Kotlin ANKO 程序编写一个包含 textview 和 EditText 的列表,因为我没有字段名称,所以我将其标识名称放入 TAG 中。都是动态的,那一刻我不知道每个EditText叫什么,我把数据放在一个变量里。

我的问题是试图获取 EditText 的值来保存它。

生成元素的代码是这样的

editText {
      hint = value --> ES EL VALOR ACTUAL EN LA DB 
      tag = col --> ESTA ES LA VARIABLE QUE TIENE EL NOMBRE POR EJEMPLO APELLIDO
      gravity = Gravity.END
      textSize = 18f
      isEnabled = estate
      textAlignment = View.TEXT_ALIGNMENT_TEXT_START
}.lparams(width = 0, height = wrapContent)
{
   weight = 0.7f
}

所以当按下保存按钮时,我需要检索视图的每个 EditText 的值

作为我保存的字段的名称,我所做的是它们中的一个,并尝试为我带来相应 EDITTEXT 的值,但它给了我一个错误,它并没有真正告诉我任何事情,但它需要我出APP

for (col in columnNames){
  if (col != "ID") {
        var inputTestText = findViewWithTag<EditText>(col).text
  }
}

col => tiene el valor compatible, por ejemplo "apellido" pero cuando paso por esa línea directamente se va de la APP sin largar ningún error

【问题讨论】:

  • 请用英文提问

标签: android kotlin anko


【解决方案1】:

使用RootView 添加所有EditText 以使用findViewWithTag 获取指定视图。它看起来像:

var inputTestText = rootView.findViewWithTag<EditText>(col).text

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2021-04-12
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多