kotlin函数重载为android

少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述kotlin函数重载为android相关的知识,希望能为你提供帮助。
我正在学习java / Kotlin并尝试编写一个简单的计算器应用程序。一切正常,直到这里我按Enter键得到我的答案。我实现了两个显示功能numberDisplay。一个显示文本视图的用户输入,另一个显示用户按Enter后对同一文本视图的答案。但是,答案不显示。如果我删除第二个显示功能并坚持只有一个(采取数组的那个)它的工作原理。但它不适用于带字符串的第二个函数。我只是因为某种原因没有显示。我知道可能有更好的方法来做到这一点,但我想弄清楚为什么重载不起作用。

package com.zzz.yyy.xximport android.graphics.Color import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import kotlinx.android.synthetic.main.activity_main.* import kotlin.collections.ArrayListclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)}var displayList = arrayListOf< String> ()fun buttEvent(view:View){ val butt = view as Button var buttID:String = "0"when(butt.id){ button13.id-> buttID ="C" button14.id-> buttID ="D" button15.id-> buttID ="/"button16.id-> buttID ="8" button17.id-> buttID ="9" button18.id-> buttID ="X" button7.id-> buttID ="7"button19.id-> buttID ="5" button20.id-> buttID ="6" button21.id-> buttID ="-" button6.id-> buttID ="4"button22.id-> buttID ="1" button23.id-> buttID ="2" button24.id-> buttID ="3" button25.id-> buttID ="+"button26.id-> buttID ="0" button27.id-> buttID ="." button28.id-> buttID ="=" button29.id-> buttID ="N" else-> buttID="null" }if(buttID == "D"){ displayList.removeAt(displayList.count()-1) } else if (buttID == "C"){ displayList.clear() }else if (buttID== "="){ equals(displayList) }else{ displayList.add(buttID) } numberDisplay(displayList)}fun equals(xthings:ArrayList< String> ){ var operation = "" var temp= ArrayList< String> () var indexoperation = 0 var finalnumber= xthings.count() var answer:String = ""for (thing in xthings) { if (thing == "X" || thing == "-" || thing == "+" || thing == "/") { indexoperation = xthings.indexOf(thing) operation = thing } } temp= xthings.joinToString("").split(operation) as ArrayList< String> when(operation){ "X"-> answer= (temp[0].toInt() * temp[1].toInt()).toString() else-> answer = "null" }numberDisplay(answer) }fun numberDisplay(arr:ArrayList< String> ){ val outputString:String = arr.joinToString("") showNumbers.text = outputString }fun numberDisplay(texts:String){ showNumbers.text = texts }}

我可能遗漏了一些明显的东西,但我无法弄清楚是什么
编辑:我通过添加第二个onclick事件使其工作。在这里使用新的等号按钮是新代码
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)}var displayList = arrayListOf< String> ()fun buttEvent(view:View){ val butt = view as Button var buttID:String = "0"when(butt.id){ button13.id-> buttID ="C" button14.id-> buttID ="D" button15.id-> buttID ="/"button16.id-> buttID ="8" button17.id-> buttID ="9" button18.id-> buttID ="X" button7.id-> buttID ="7"button19.id-> buttID ="5" button20.id-> buttID ="6" button21.id-> buttID ="-" button6.id-> buttID ="4"button22.id-> buttID ="1" button23.id-> buttID ="2" button24.id-> buttID ="3" button25.id-> buttID ="+"button26.id-> buttID ="0" button27.id-> buttID ="."button29.id-> buttID ="N" else-> buttID="null" }if(buttID == "D"){ displayList.removeAt(displayList.count()-1) } else if (buttID == "C"){ displayList.clear() }else{ displayList.add(buttID) } numberDisplay(displayList)}fun buttEqual(view: View){var operation = "" var temp= ArrayList< String> () var indexoperation = 0 var finalnumber= displayList.count() var answer:String = ""for (thing in displayList) { if (thing == "X" || thing == "-" || thing == "+" || thing == "/") { indexoperation = displayList.indexOf(thing) operation = thing } } temp= displayList.joinToString("").split(operation) as ArrayList< String> when(operation){ "X"-> answer= (temp[0].toInt() * temp[1].toInt()).toString() else-> answer = "null" }numberDisplayx(answer)}fun numberDisplay(arr:ArrayList< String> ){ val outputString:String = arr.joinToString("") showNumbers.text = outputString }fun numberDisplayx(texts:String){ showNumbers.text = texts.toString() }}

【kotlin函数重载为android】我仍然想知道为什么它第一次不起作用
答案尝试使用vararg的功能:
fun numberDisplay(vararg texts:String) { ... }

叫它:
var answer:String = "" numberDisplay(answer)

要么
var displayList = arrayListOf< String> () numberDisplay(*displayList)


    推荐阅读