2017-07-07 115 views
-2
設置文本,突出重點,誤差在EDITTEXT

我搜索大量互聯網上關於我如何能在與科特林Android的使用上editText(s)setFocusable()setText()setError()等等等等方法(我知道的事實,我們可以使用在Java中上述方法),但我無法找到確切的解決方案,將爲我工作。如何在android系統與科特林

我使用 1)排球的HTTP調用 2)與版本的Android工作室科特林插件= '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 
    } 
+0

請保持您的代碼小。目前尚不清楚您遇到問題的位置,只需添加包含問題的必要代碼即可。 – guenhter

+0

@guenhter可以告訴我如何使用setText(),setText()在android中的editText與KOTLIN ... n也setError() –

+0

不,我沒有進入android並不能給你的建議,但如果你做了一個更好的描述(https://stackoverflow.com/help/mcve最小,完整和可驗證的例子),任何人都可以幫助你,而不僅僅是降低你的問題。 – guenhter

回答

1

這很簡單。假設etEmailEditText。您可以設置文字這樣

etEmail?.setText("some text") 

而對於錯誤,您可以使用此

etEmail?.error = "This is error" 

而對於集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) 
    } 
} 
+0

etEmail?.isFocusable = false工作正常,但其他兩種方法不起作用。 –

+0

我剛剛在我的項目中測試了這些,這些工作正常。 –

+0

我附上了屏幕截圖,你可以查看它。必須有一些你在代碼中沒有做的事。你能發佈相關的代碼嗎? –