英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论