java 不要连接用 setText 显示的文本。使用带有占位符的资源字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35113625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do not concatenate text displayed with setText. Use resource string with placeholders
提问by Shashwat
I am a newbie in android development,
I want to setText
a number, I am facing this problem and tried all possible ways to solve it.
我是android开发的新手,我想要setText
一个数字,我面临这个问题并尝试了所有可能的方法来解决它。
Please provide me the codes to solve this problem
请给我提供解决这个问题的代码
Codes are as follows:
代码如下:
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);
errors in last 8th line to last 4th line.
最后 8 行到最后 4 行中的错误。
回答by Dheerendra Jeevani
the simple solution to your problem is:
您的问题的简单解决方案是:
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));
what android studio is suggesting you is that if you want to append something in your textview then you have to use placeholders.
android studio 建议你的是,如果你想在你的 textview 中附加一些东西,那么你必须使用占位符。
example of using placeholders is as follows:
使用占位符的示例如下:
file: strings.xml
文件:strings.xml
...
<string name="part_a">part a value = %1$s.</string>
...
file: activity_main.xml
文件: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
文件名: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 );
...
i hope this clears your doubt.Formatting strings: android developer
我希望这可以消除您的疑虑。格式化字符串:android developer
回答by Shamas S - Reinstate Monica
It means that instead of doing it the way you are doing it right now, you should declare Strings in your strings.xml. For example for correct Answer, you can create a string which will have $1%s
. And you can use String.format function to use use your correct answer.
这意味着您应该在strings.xml 中声明Strings,而不是按照您现在的方式进行操作。例如,对于正确的答案,您可以创建一个字符串,其中包含$1%s
. 您可以使用 String.format 函数来使用您的正确答案。
回答by Ivan Wooll
The warning is against concatenating (joining) strings inside the setText() method. As you are assigning integer values the best way to do this is as follows textObjectPartA.setText(String.valueOf(partA));
该警告是针对在 setText() 方法中连接(连接)字符串的。当您分配整数值时,执行此操作的最佳方法如下textObjectPartA.setText(String.valueOf(partA));
回答by twarped
try making a variable containing your string. then using the variable as your text.
like: String partA_to_text= ""+partA.toString
尝试创建一个包含您的字符串的变量。然后使用该变量作为您的文本。喜欢:String partA_to_text= ""+partA.toString