看板 AndroidDev 關於我們 聯絡資訊
大家好小弟新手 目前透過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 直接等於零? 謝謝大家! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 121.254.76.49 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1530687721.A.3F1.html
kobe8112: fun 函式名稱(參數名稱:參數型態) 07/04 16:36
aids61517: 1. countMe用在哪裡?感覺像是在xml設定onClick 07/04 16:39
aids61517: 如果是的話那個view代表被按下的View 07/04 16:39
kobe8112: showCountTextView是一個View(TextView),text是其屬性 07/04 16:40
aids61517: 2. .text 的意思是宣告showCountTextView 是字串嗎? 07/04 16:40
kobe8112: 單純想設成0直接 showCountTextView.text="0" 即可 07/04 16:41
aids61517: 答案為No,他原本是showCountTextView.setText(xxx) 07/04 16:41
aids61517: 純新手建議先從Java上手 07/04 16:41
paul7322000: 看起來兩個fun都是在xml宣告onclick的 所以view不能 07/04 21:26
paul7322000: 少,少了在oncreat的時候就會crash了 07/04 21:26
james732: 你對Kotlin的基本概念太欠缺了,先打基礎吧 07/05 01:24
謝謝以上大大回覆,這兩個功能都是在XML 直接設定onClick沒錯。 因為一開始學Kotlin,但書上的東西對我來說太虛幻了。 所以我想透過做中學,才會遇到這麼基礎的問題卻想不出來。 接下來會惡補一下,基礎的部分。再次感謝大大們的回覆。得到很好的解答。 ※ 編輯: bemaduro (121.254.76.49), 07/05/2018 11:16:12
y3k: 你有寫過Java嗎...? 這些問題看起來都是連Java都不會在問的@@ 07/05 12:16
aids61517: 建議純新手先從Java開始,至少做個半年以上再考慮用 07/05 13:00
aids61517: Kotlin,不然什麼時候用.text setText之類的問題會讓你 07/05 13:00
aids61517: 學習方向失焦 07/05 13:00
bemaduro: 謝謝兩位前輩回應,那我接下來會去Udemy找個Java的課程 07/05 15:25