Pra*_*ani 122 android concatenation string-concatenation textview
我通过以下方式使用setText()设置文本.
prodNameView.setText("" + name);
prodOriginalPriceView.setText("" + String.format(getString(R.string.string_product_rate_with_ruppe_sign), "" + new BigDecimal(price).setScale(2, RoundingMode.UP)));
Run Code Online (Sandbox Code Playgroud)
在此首先一个是使用简单,二一个是设置文本与文本格式.
Android Studio非常有趣,我使用了Menu Analyze -> Code Cleanup,我得到了以上两行的建议.
不要连接用setText显示的文本.使用带占位符的资源字符串.少...(Ctrl + F1)
调用TextView#setText时:
- 永远不要调用Number#toString()来格式化数字; 它不会正确处理分数分隔符和特定于语言环境的数字.请考虑使用具有适当格式规范(%d或%f)的String#格式.
- 不要传递字符串文字(例如"Hello")来显示文本.硬编码文本无法正确翻译为其他语言.请考虑使用Android资源字符串.
- 不要通过连接文本块来构建消息.此类消息无法正确翻译.
我能为此做些什么?任何人都可以帮助解释这是什么,我该怎么办?
Bla*_*elt 259
Resource具有getString的get overloaded版本,它采用以下varargs类型Object:getString(int,java.lang.Object ...).如果在strings.xml中正确设置了字符串,并使用正确的占位符,则可以使用此版本检索最终字符串的格式化版本.例如
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Run Code Online (Sandbox Code Playgroud)
运用 getString(R.string.welcome_message, "Test", 0);
android会返回一个String
Hello Test! you have 0 new messages
Run Code Online (Sandbox Code Playgroud)
关于 setText("" + name);
你的第一个例子,prodNameView.setText("" + name);对我没有任何意义.TextView能够处理空值.如果name为null,则不会绘制任何文本.
Ris*_*esh 25
不要在接受的答案中与 %1 $ s和%2 $ d混淆.这里有一些额外的信息.
%[
argument_index$]format_specifier
例
我们将创建以下格式化字符串,其中以编程方式插入灰色部分.
你好
Test!你有0新消息
你的string resource:
<string name ="welcome_messages">你好,
%1$s!你有%2$d新消息</ string>
做string substitution下面给出:
getString(R.string.welcome_message
"Test",,0);
注意:
小智 13
我遇到了相同的lint错误消息并以这种方式解决了它.
最初我的代码是:
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + quantity);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Do not concatenate text displayed with setText. Use resource string with placeholders.
Run Code Online (Sandbox Code Playgroud)
所以,我添加了这个 strings.xml
<string name="blank">%d</string>
Run Code Online (Sandbox Code Playgroud)
这是我的初始""+我的号码(数量)的占位符.
注意:我的quantity变量先前已定义,是我想要附加到字符串的内容.结果我的代码是
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(getString(R.string.blank, quantity));
}
Run Code Online (Sandbox Code Playgroud)
在此之后,我的错误消失了.应用程序中的行为没有改变,我的数量继续显示,因为我现在想要它没有lint错误.
Ash*_*kol 10
不串接你的文字里的setText()方法,串联你在想什么都串并把你内部的字符串值的setText()方法.
例如:正确的方式
int min = 120;
int sec = 200;
int hrs = 2;
String minutes = String.format("%02d", mins);
String seconds = String.format("%02d", secs);
String newTime = hrs+":"+minutes+":"+seconds;
text.setText(minutes);
Run Code Online (Sandbox Code Playgroud)
不要在setText()内部连接
text.setText(hrs+":"+String.format("%02d", mins)+":"+String.format("%02d", secs));
Run Code Online (Sandbox Code Playgroud)
您应该检查此线程并使用类似他的占位符(未测试)
<string name="string_product_rate_with_ruppe_sign">Price : %1$d</string>
String text = String.format(getString(R.string.string_product_rate_with_ruppe_sign),new BigDecimal(price).setScale(2, RoundingMode.UP));
prodOriginalPriceView.setText(text);
Run Code Online (Sandbox Code Playgroud)
小智 9
别生气,这太简单了。
String firstname = firstname.getText().toString();
String result = "hi "+ firstname +" Welcome Here";
mytextview.setText(result);
Run Code Online (Sandbox Code Playgroud)
小智 6
我通过使用 String.format 修复了它
之前 :
textViewAddress.setText("Address"+address+"\n"+"nCountry"+"\n"+"City"+"city"+"\n"+"State"+"state")
Run Code Online (Sandbox Code Playgroud)
后 :
textViewAddress.setText(
String.format("Address:%s\nCountry:%s\nCity:%s\nState:%s", address, country, city, state));
Run Code Online (Sandbox Code Playgroud)
问题是因为您附加""在每个字符串的开头。
lint 将扫描传递给的参数setText并生成警告,在您的情况下,以下警告是相关的:
不要通过连接文本块来构建消息。此类消息无法正确翻译。
当您将每个字符串与"".
删除此连接,因为您传递的参数已经是文本。此外,您可以.toString()在其他任何地方使用if at all required 而不是将您的字符串与""
| 归档时间: |
|
| 查看次数: |
70398 次 |
| 最近记录: |