[英]how to set text, focus, error on editText in android with Kotlin
我在互联网上大量搜索有关如何使用Kotlin在android的editText(s)
上使用setFocusable()
, setText()
, setError()
等方法的方法(我知道我们可以在Java中使用上述方法)我找不到适合我的确切解决方案。
我正在使用1.)Volley进行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
}
这很简单。 让我们假设etEmail
是EditText
。 您可以这样设置文本
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.