[問題] 新手提問

作者: 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 直接等於零?
謝謝大家!
作者: aids61517 (奇歐巴肯)   2018-07-05 13:00:00
建議純新手先從Java開始,至少做個半年以上再考慮用Kotlin,不然什麼時候用.text setText之類的問題會讓你學習方向失焦
作者: y3k (激流を制するは静水)   2018-07-05 12:16:00
你有寫過Java嗎...? 這些問題看起來都是連Java都不會在問的@@
作者: bemaduro (superhotblood)   2018-07-05 15:25:00
謝謝兩位前輩回應,那我接下來會去Udemy找個Java的課程
作者: kobe8112 (小B)   2018-07-04 16:36:00
fun 函式名稱(參數名稱:參數型態)
作者: aids61517 (奇歐巴肯)   2018-07-04 16:39:00
1. countMe用在哪裡?感覺像是在xml設定onClick如果是的話那個view代表被按下的View
作者: kobe8112 (小B)   2018-07-04 16:40:00
showCountTextView是一個View(TextView),text是其屬性
作者: aids61517 (奇歐巴肯)   2018-07-04 16:40:00
2. .text 的意思是宣告showCountTextView 是字串嗎?
作者: kobe8112 (小B)   2018-07-04 16:41:00
單純想設成0直接 showCountTextView.text="0" 即可
作者: aids61517 (奇歐巴肯)   2018-07-04 16:41:00
答案為No,他原本是showCountTextView.setText(xxx)純新手建議先從Java上手
作者: paul7322000 (風飄雪夜)   2018-07-04 21:26:00
看起來兩個fun都是在xml宣告onclick的 所以view不能少,少了在oncreat的時候就會crash了
作者: james732 (好人超)   2018-07-05 01:24:00
你對Kotlin的基本概念太欠缺了,先打基礎吧

Links booklink

Contact Us: admin [ a t ] ucptt.com