如何解决在Android的Kotlin中减去用户输入的工作方式
我知道这是错误的,请帮忙搜索Yt各处,一无所获:(需要学习如何将数学与变量结合使用
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_calc.*
class Calc : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_calc)
var need = findViewById<TextView>(R.id.t_need)
var money = findViewById<TextView>(R.id.t_money)
var debt = findViewById<TextView>(R.id.t_debt)
var b_calc = findViewById<TextView>(R.id.b_calc)
b_calc.setonClickListener {
val diffrence = Money - debt
need.text = diffrence
}
}
}
解决方法
因此,当您从textview
(通常是Charsequence
)中获取文本时,首先需要将其Parse
保留为int
。一个例子是
var moneyText = findViewById<TextView>(R.id.t_money)
var money = moneyText.toString().toInt()
您可以在这里money - debt
进行操作,或者使用Kotlin进行money.minus(debt)
,您可以签出Kotlin算术函数here。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。