将文本添加到文本视图中

huangapple go评论85阅读模式
英文:

Appending text to textview

问题

以下是翻译好的内容:

有一个带有以下 onClick 事件的按钮:

fun add(view: View) {
    txt.append((view as Button).text)
}

但是在使用 += 运算符时出现错误。

fun add(view: View) {
    txt.text += (view as Button).text   //error
}

第二段代码有什么问题?

英文:

There's a button with the following onClick event:

fun add(view: View) {
    txt.append((view as Button).text)
}

But I get an error on using += operator.

fun add(view: View) {
    txt.text += (view as Button).text   //error
}

What's wrong with the second code??

答案1

得分: 1

这是使用字符串插值的解决方案:

private fun add(view: View) {
    txt.text = "${txt.text} ${(view as Button).text}"
}

这是官方文档

阅读一下,如果这不能解决你的问题,请告诉我,祝你编码愉快!

编辑

关于你的问题:我认为这个是我在查找时找到的唯一“有效”答案。

英文:

This is the solution with string interpolation:

private fun add(view: View) {
    txt.text = "${txt.text} ${(view as Button).text}"
}

This is the official documentation.

Give it a read and let me know if this doesn't solve your problem, happy coding!

EDIT

Regarding your question: i think this is the only "valid" answer I found looking around.

huangapple
  • 本文由 发表于 2023年8月9日 14:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定