不要使用setText连接显示文本。请使用具有占位符的资源字符串。

10

我是Android开发的新手,我想在setText中提供一个数字,但我遇到了这个问题并尝试了很多方法来解决它。

代码如下:

public class GameActivity extends Activity implements View.OnClickListener{
    int correctAnswer;
    Button buttonObjectChoice1;
    Button buttonObjectChoice2;
    Button buttonObjectChoice3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //here we initialize all our varibles
    int partA = 9;
    int partB = 2;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;
    TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
    TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
    buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
    buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
    buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
    textObjectPartA.setText("" + partA);
    textObjectPartB.setText("" + partB);
    buttonObjectChoice1.setText("" + correctAnswer);
    buttonObjectChoice2.setText("" + wrongAnswer1);
    buttonObjectChoice3.setText("" + wrongAnswer2);
    buttonObjectChoice1.setOnClickListener(this);
    buttonObjectChoice2.setOnClickListener(this);
    buttonObjectChoice3.setOnClickListener(this);

最后第8行到倒数第4行有错误。

错误图片


问题的截图将清楚地展示我所面临的情况。请点击以下链接查看截图:http://i.stack.imgur.com/nVdSz.png - Shashwat
你需要使用字符串代替“硬编码文本”。 - Stanojkovic
1
继续保持你的工作,只有在需要翻译你的应用程序时才是重要的。 - Stanojkovic
3个回答

13

你问题的简单解决方案是:

textObjectPartA.setText(String.valueOf(partA));
textObjectPartB.setText(String.valueOf(partB));
buttonObjectChoice1.setText(String.valueOf(correctAnswer));
buttonObjectChoice2.setText(String.valueOf(wrongAnswer1));
buttonObjectChoice3.setText(String.valueOf(wrongAnswer2));

Android Studio建议您使用占位符来向TextView添加内容。

使用占位符的示例如下:

文件:strings.xml

...
<string name="part_a">part a value = %1$s.</string>
...

文件:activity_main.xml

...
<TextView
    android:id="@+id/R.id.textPartA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/part_a" />
...

文件名:GameActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //here we initialize all our varibles
    int partA = 9;
    int partB = 2;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;
    TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
    TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
    buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
    buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
    buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);

    Resources res = getResources();
    String partA_text = String.format(res.getString(R.string.part_a), partA);
    textObjectPartA.setText(partA_text );
...
我希望这能消除你的疑虑。格式化字符串:Android开发者

为什么我们需要 Resources res = getResources(); ?我尝试过在没有它的情况下运行代码,也可以工作,这会有什么后果吗? - Moisoni Ioan

0

警告是不要在setText()方法内连接字符串。由于您正在分配整数值,因此最好的方法是按如下方式执行:textObjectPartA.setText(String.valueOf(partA));


-2

尝试创建一个包含字符串的变量,然后使用该变量作为您的文本。 例如:String partA_to_text = ""+partA.toString


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接