我是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行的错误。
答案 0 :(得分: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));
Android工作室建议你的是,如果你想在textview中添加一些内容,那么你必须使用占位符。
使用占位符的示例如下:
file:strings.xml
...
<string name="part_a">part a value = %1$s.</string>
...
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" />
...
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 );
...
我希望这可以解除你的怀疑。stateless protocol
答案 1 :(得分:0)
这意味着你不应该像现在这样做,而应该在strings.xml中声明Strings。例如,对于正确的答案,您可以创建一个具有With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1",Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, Height:=15)
With .Object
.AddItem "Yes"
.AddItem "No"
End With
End With
的字符串。并且您可以使用String.format函数来使用您正确的答案。
答案 2 :(得分:0)
警告是禁止在setText()方法中连接(连接)字符串。在分配整数值时,最好的方法如下textObjectPartA.setText(String.valueOf(partA));
答案 3 :(得分:-1)
尝试创建一个包含您的字符串的变量。然后使用变量作为文本。
像:String partA_to_text= ""+partA.toString