kotlin函数重载为android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kotlin函数重载为android相关的知识,希望对你有一定的参考价值。

我正在学习Java / Kotlin并尝试编写一个简单的计算器应用程序。一切正常,直到这里我按Enter键得到我的答案。我实现了两个显示功能numberDisplay。一个显示文本视图的用户输入,另一个显示用户按Enter后对同一文本视图的答案。但是,答案不显示。如果我删除第二个显示功能并坚持只有一个(采取数组的那个)它的工作原理。但它不适用于带字符串的第二个函数。我只是因为某种原因没有显示。我知道可能有更好的方法来做到这一点,但我想弄清楚为什么重载不起作用。

package com.zzz.yyy.xx

import 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.ArrayList

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 ="."
            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()
    }

}

我仍然想知道为什么它第一次不起作用

答案

尝试使用vararg的功能:

fun numberDisplay(vararg texts:String) {
    ...
}

叫它:

var answer:String = ""
numberDisplay(answer)

要么

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

以上是关于kotlin函数重载为android的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin 扩展函数和运算符重载[第一行代码 Kotlin 学习笔记]

Kotlin扩展函数与重载操作符解析

Kotlin扩展函数与重载操作符解析

Kotlin Android Studio - setContenView - 绑定(片段)

如何使用 Kotlin 从 Android 中的片段访问另一个片段?

Kotlin:找不到符号类片段或其他 android 类