如何使用 Kotlin 在 android 中的 editText 上设置文本、焦点和错误
how to set text, focus, error on editText in android with Kotlin
我在互联网上大量搜索关于如何在 android 中使用 setFocusable()
、setText()
、setError()
等方法和 Kotlin(我知道我们可以在 java) 中使用上述方法,但我无法找到适合我的确切解决方案。
我正在使用
1.) 凌空调用 http
2.) android studio 版本 = '1.1.3-2' 的 kotlin 插件
3.) anko 图书馆
Problems that i am facing when app is running :
1.)The setError() method is not getting called.
2.)i am not able to use setText() and setFocus() on editText.
请注意,我需要 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)
}
}
我在互联网上大量搜索关于如何在 android 中使用 setFocusable()
、setText()
、setError()
等方法和 Kotlin(我知道我们可以在 java) 中使用上述方法,但我无法找到适合我的确切解决方案。
我正在使用 1.) 凌空调用 http 2.) android studio 版本 = '1.1.3-2' 的 kotlin 插件 3.) anko 图书馆
Problems that i am facing when app is running : 1.)The setError() method is not getting called. 2.)i am not able to use setText() and setFocus() on editText.
请注意,我需要 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)
}
}