如何输出/打印到TextView的值
我想输出/打印分数值的TextView,但我得到了“期待会员申报”错误。
TextView XML:
function:
var PlayerOneScore=12 var score: TextView = findViewById(R.id.playerOneScore) as TextView score.setText(PlayerOneScore)
TextView.setText的签名与int
(对于android字符串)或CharSequence
对于普通或格式化的字符串)兼容。 在你的情况下,你正在分配一个Int
然后第一个签名被调用。
为了避免这种情况,您必须将值转换为String
以在textView中显示值12
。 这可以实现如下:
myTextView.setText(myIntValue.toString())
考虑到kotlin为getText()/setText()
提供属性访问语法。 使用它你可以避免犯同样的错误。
myTextView.text = myIntValue //an error will be displayed because int isn't assignable for CharSequence myTextView.text = myIntValue.toString() // Good !
在你的情况下:
score.text = PlayerOneScore.toString()
PlayerOneScore.toString()或tv.text =“$ PlayerOneScore”
你不会想使用setText iirc。
score.text = PlayerOneScore