我遇到了问题,Android Studio 提醒我使用 string.xml 来设置文本。下面给了我提示:不要连接用 setText 显示的文本。使用带占位符的资源字符串。
public int points = 0;
public TextView tv_points;
tv_points.setText("Points: " + points);
好吧,如果我将字符串 xml 与此代码一起使用,它不是我想要的:
<string name="points_string">Points: </string>
public int points = 0;
public TextView tv_points;
tv_points.setText((R.string.points_string) + points);
它没有带来任何错误,也没有提示,但它是错误的。我没有得到设置点的预期效果。
最佳答案
您需要使用 strings.xml
格式化字符串像这样:
<string name="points_string">Points: %1$d</string>
然后你可以像这样使用它:
tv_points.setText(getResources().getString(R.string.points_string, points));
关于java - 如何在 Java Android Studio 中使用 String 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48589219/