如何在Android中使用Kotlin设置editText上的文本,焦点,错误

时间:2017-07-07 05:29:59

标签: android kotlin anko

我在互联网上大量搜索了我如何使用setFocusable()setText()setError()等方法在机器人editText(s)上使用Kotlin(我知道我们的事实)可以在java中使用上述方法,但我无法找到适合我的确切解决方案。

我正在使用 1.)用于http呼叫的凌空 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
    }

1 个答案:

答案 0 :(得分:3)

这很简单。假设etEmailEditText。您可以像这样设置文本

etEmail?.setText("some text")

对于错误,您可以使用此

etEmail?.error = "This is error"

对于set foucus,你可以尝试这个,但我不确定。

etEmail?.isFocusable = false

我希望这会对你有所帮助。

检查上述代码的工作屏幕截图。

enter image description here

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)
    }
}