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

Sha*_*wat 6 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);
Run Code Online (Sandbox Code Playgroud)

最后8行到最后4行的错误.

错误图片

Dhe*_*ani 8

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

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));
Run Code Online (Sandbox Code Playgroud)

android工作室建议你的是,如果你想在textview中添加一些内容,那么你必须使用占位符.

使用占位符的示例如下:

file:strings.xml

...
<string name="part_a">part a value = %1$s.</string>
...
Run Code Online (Sandbox Code Playgroud)

file: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" />
...
Run Code Online (Sandbox Code Playgroud)

filename: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 );
...
Run Code Online (Sandbox Code Playgroud)

我希望这能清除你的怀疑.格式化字符串:android开发人员


Iva*_*oll 0

该警告是针对在 setText() 方法内连接(连接)字符串的警告。当您分配整数值时,最好的方法如下textObjectPartA.setText(String.valueOf(partA));