【问题标题】:how to set text, focus, error on editText in android with Kotlin如何使用 Kotlin 在 android 中的 editText 上设置文本、焦点、错误
【发布时间】:2017-07-07 05:29:59
【问题描述】:

我在互联网上大量搜索关于如何在带有 Kotlin 的 android 中使用 setFocusable()setText()setError() 等方法(我知道我们可以在 java 中使用上述方法的事实) 但我找不到适合我的确切解决方案。

我正在使用 1.) 用于 http 调用的 Volley 2.) 用于 android studio 的 kotlin 插件,版本 = '1.1.3-2' 3.) anko 库

应用程序运行时我面临的问题: 1.) setError() 方法没有被调用。 2.)我无法在 editText 上使用 setText() 和 setFocus()。

请注意,我需要 Kotlin 而非 Java 中的解决方案。

提前致谢!

private fun askAppointment() {

    if (editTextPersonName?.text.isNullOrEmpty()) {
        editTextPersonName?.error ="Person Name cannot be empty."
        return
    } else if (editTextPersonMobile?.text.isNullOrEmpty()) {
        editTextPersonMobile?.error = "Person Mobile cannot be empty."
        return
    } else if (editTextPersonEmail?.text.isNullOrEmpty()) {
        editTextPersonEmail?.error = "Person Email cannot be empty."
        return
    } else if (editTextSubject?.text.isNullOrEmpty()) {
        editTextSubject?.error = "Subject cannot be empty."
        return
    } else if (editTextDescription?.text.isNullOrEmpty()) {
        editTextDescription?.error = "Description cannot be empty."
        return
    } else if (editTextAppointmentDate?.text.isNullOrEmpty()) {
        editTextAppointmentDate?.error = "Appointment Date cannot be empty."
        return
    } else if (editTextAppointmentTime?.text.isNullOrEmpty()) {
        editTextAppointmentTime?.error = "Appointment Time cannot be empty."
        return
    }

【问题讨论】:

  • 请保持您的代码小。不清楚问题出在哪里,所以只需添加包含问题的必要代码即可。
  • @guenhter 可以告诉我如何在带有 KOTLIN 的 android 中的 editText 上使用 setFocus()、setText()...n 还有 setError()
  • 不,我不是android,不能给你建议,但如果你做一个更好的描述(stackoverflow.com/help/mcve最小、完整和可验证的例子),任何人帮助你的机会都会更高而不仅仅是降低你的问题。

标签: android kotlin anko


【解决方案1】:

这很简单。让我们假设etEmailEditText。你可以这样设置文字

etEmail?.setText("some text")

对于错误,您可以使用它

etEmail?.error = "This is error"

对于固定焦点,你可以试试这个,但我不确定。

etEmail?.isFocusable = false

希望对你有帮助。

查看上述代码的工作截图。

askAppointment()中使用这个逻辑

private fun askAppointment() {

    if (editTextPersonName?.text.isNullOrEmpty()) {
        editTextPersonName?.error = "Person Name cannot be empty."
        return
    } else if (editTextPersonMobile?.text.isNullOrEmpty()) {
        editTextPersonMobile?.error = "Person Mobile cannot be empty."
        return
    } else if (editTextPersonEmail?.text.isNullOrEmpty()) {
        editTextPersonEmail?.error = "Person Email cannot be empty."
        return
    } else if (editTextSubject?.text.isNullOrEmpty()) {
        editTextSubject?.error = "Subject cannot be empty."
        return
    } else if (editTextDescription?.text.isNullOrEmpty()) {
        editTextDescription?.error = "Description cannot be empty."
        return
    } else if (editTextAppointmentDate?.text.isNullOrEmpty()) {
        editTextAppointmentDate?.error = "Appointment Date cannot be empty."
        return
    } else if (editTextAppointmentTime?.text.isNullOrEmpty()) {
        editTextAppointmentTime?.error = "Appointment Time cannot be empty."
        return
    } else {
        //creating volley string request
        val stringRequest = object : StringRequest(Request.Method.POST, URL,
                Response.Listener<String> { response ->
                    try {
                        val jsonObject = JSONObject(response)
                        val feedback = jsonObject.getString("response")
                        toast("$feedback")
                        //finish()       //finish Activity after sending request
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                },
                object : Response.ErrorListener {
                    override fun onErrorResponse(volleyError: VolleyError) {
                        toast("error :(")
                    }
                }) {
            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("personName", editTextPersonName?.text.toString())
                params.put("personMobile", editTextPersonMobile?.text.toString())
                params.put("personEmail", editTextPersonEmail?.text.toString())
                params.put("subject", editTextSubject?.text.toString())
                params.put("description", editTextDescription?.text.toString())
                params.put("appointMentDate", editTextAppointmentDate?.text.toString())
                params.put("appointMentTime", editTextAppointmentTime?.text.toString())
                return params
            }
        }

        //adding request to queue
        AppController.instance?.addToRequestQueue(stringRequest)
    }
}

【讨论】:

  • etEmail?.isFocusable = false 工作正常,但其他两种方法不起作用。
  • 我刚刚在我的项目中测试了这些,它们工作正常。
  • 我已附上屏幕截图,您可以查看。你的代码中一定有一些你没有做对的事情。能发下相关代码吗?
  • 我正在做这样的事情if (editTextPersonName?.text.toString() == "") { editTextPersonName?.error = "Person Name cannot be empty." return }
  • 您的支票有误。 if(etEmail?.text.isNullOrEmpty()){ etEmail?.setError("Some error") }用这个
猜你喜欢
  • 2017-11-13
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
相关资源
最近更新 更多