Android TextView.setText() 实现字符串(String)+数值(Integer)作为参数

在我们要实现字符串+数值作为参数输入setText()中时,很容易想到如下代码:

int sayHelloWorld = 3;
tv.setText(R.string.hello_world + sayHelloWorld);

但运行后发现这种写法会产生错误。
解决办法是将数值变量转化为字符串变量,与另一字符串组合后再作为参数输入setText()中,例如:

int sayHelloWorld = 3;
String helloWorldCombination = getString(R.string.hello_world) + String.valueOf(sayHelloWorld);
tv.setText(helloWorldCombination);

这样就没有问题了。

你可能感兴趣的:(#,Java,#,Android)