java - 不要连接使用 setText 显示的文本。使用带占位符的资源字符串

标签 java android settext

我是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 行。

Error image

最佳答案

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

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 );
...

我希望这能消除您的疑虑。 Formatting strings: android developer

关于java - 不要连接使用 setText 显示的文本。使用带占位符的资源字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113625/

相关文章:

java - A* star openlist 未按预期工作

java - 如何分别获取每个元素的所有属性?

java - 记录时异常有什么好处?

android - 如何在 reactjs 中更改 Android 主题颜色 (colorPrimaryDark)

android - 如何从 Android Studio 中的 Assets 中读取文本文件?

android - 如何在 android studio 中包含应用内图标选择器?

java - 如何在 Javaeditor jTable 中为单元格设置文本?

java - Android Java : . setText() 每 x 毫秒

java - textView setText() NullPointerException

Javamail - 无法连接到 SMTP 主机