作者:
bemaduro (superhotblood)
2018-07-04 15:01:58大家好小弟新手 目前透過Udemy自學 Android
目前在做一個 Counter 的小程式 程式碼如下
有幾個疑問想要提問
fun countMe (view: View) {
括號內的(view: View) 的意義是??
不宣告的話app 又會crash
// Get the text view.
val showCountTextView = number
// Get the value of the text view.
val countString = showCountTextView.text.toString()
//Convert value to a number and increment it
var count: Int = Integer.parseInt(countString)
count++
// Display the new value in the text view.
showCountTextView.text = count.toString()
.text 的意思是宣告showCountTextView 是字串嗎?
}
fun reSet (view: View) {
// Get the text view.
val showCountTextView = number
//Convert value to 0
val zeroing = 0
// Display the new value in the text view.
showCountTextView.text = zeroing.toString()
}
這個功能的目的是要讓數字歸零,已修改讓他更簡潔。有更直接的做法嗎?
例如讓number 這個TextView 直接等於零?
謝謝大家!
建議純新手先從Java開始,至少做個半年以上再考慮用Kotlin,不然什麼時候用.text setText之類的問題會讓你學習方向失焦
作者:
y3k (激流を制するは静水)
2018-07-05 12:16:00你有寫過Java嗎...? 這些問題看起來都是連Java都不會在問的@@
作者:
bemaduro (superhotblood)
2018-07-05 15:25:00謝謝兩位前輩回應,那我接下來會去Udemy找個Java的課程
1. countMe用在哪裡?感覺像是在xml設定onClick如果是的話那個view代表被按下的View
showCountTextView是一個View(TextView),text是其屬性
2. .text 的意思是宣告showCountTextView 是字串嗎?
單純想設成0直接 showCountTextView.text="0" 即可
答案為No,他原本是showCountTextView.setText(xxx)純新手建議先從Java上手
看起來兩個fun都是在xml宣告onclick的 所以view不能少,少了在oncreat的時候就會crash了