gpt4 book ai didi

android - 如何使用 Kotlin 在 android 中的 editText 上设置文本、焦点、错误

转载 作者:行者123 更新时间:2023-12-02 13:03:19 25 4
gpt4 key购买 nike

我在 Internet 上大量搜索关于如何在 editText(s) in android with Kotlin(我知道我们可以在 java 中使用上述方法)但我无法找到适合我的确切解决方案。

我正在使用1.) Volley 调用 http2.) kotlin plugin for android studio with version = '1.1.3-2'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
}

最佳答案

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

etEmail?.setText("some text")

对于错误你可以使用这个

etEmail?.error = "This is error"

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

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

关于android - 如何使用 Kotlin 在 android 中的 editText 上设置文本、焦点、错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44963165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com