Kotlin中是否有与TextViews上的“ + =”运算符等效的东西?

Lez*_*man -4 android kotlin

我想将文本保留在文本视图中并添加0 我试过txtCalc.text =“文本” +“ 0”,它不起作用

zsm*_*b13 5

如果您在中读取了当前文本TextView,则会得到一个CharSequence,在将任何内容串联之前,必须将其转换为字符串:

textView.text = textView.text.toString() + "0"
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用以下append方法TextView

textView.append("0")
Run Code Online (Sandbox Code Playgroud)

或者,如果您确实想使用+=,则可以在以下位置创建自己的扩展名TextView

inline operator fun TextView.plusAssign(text: CharSequence) = append(text)

textView += "0"
Run Code Online (Sandbox Code Playgroud)