英文:
Converting regex %1$d to string in Android
问题
我有以下内容需要翻译:
我有以下内容需要翻译:
我有以下内容需要翻译:
我有以下内容需要翻译:
我有以下内容需要翻译:
抱歉,由于您的要求,我只能提供翻译后的内容。如果您需要进一步的帮助,请随时提问。
英文:
I am having <string name="monthly_savings_other">Monthly savings\n$%1$d</string>
and i know that because is %1$d
i can not display it like a string.But i don't know how to formatting properly,although there is a lot examples on internet i just can wrap my head around it.I do not know how to take %1$d
from string folder in android an formatting it.
public void binding(final OtherAccModel modelClass) {
binding.setOther(modelClass);
binding.executePendingBindings();
setSpannableTextWithColor(binding.policyTV, R.string.policy_account_number_other, modelClass.getPolicy(), Color.BLACK);
setSpannableTextWithColor(binding.companyNameTV,R.string.company_name_other,modelClass.getCompanyName(), Color.BLACK);
setSpannableTextWithColor(binding.monthlySavingsTV,R.string.monthly_savings_other, String.valueOf(modelClass.getMonthlySavings()), Color.BLACK);
setSpannableTextWithColor(binding.rorTV,R.string.return_of_value_other, String.valueOf(modelClass.getRor()), Color.BLACK);
setSpannableTextWithColor(binding.accNameTV,R.string.type_other,modelClass.getType(), Color.BLACK);
setSpannableTextWithColor(binding.accValueTV,R.string.current_value_other, String.valueOf(modelClass.getCurrentValue()), Color.BLACK);
}
void setSpannableTextWithColor(TextView view, int resourceId, String string, int color) {
if (string == null)
return;
String fulltext = context.getResources().getString(resourceId, string);
String part = string;
SpannableString str = new SpannableString(fulltext);
str.setSpan(new ForegroundColorSpan(color), fulltext.length() - part.length(), fulltext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
view.setText(str);
}
on line String fulltext = context.getResources().getString(resourceId, string);
i am getting error
java.util.IllegalFormatConversionException: d != java.lang.String
答案1
得分: 1
使用%1$s代替%1$d。
d用于十进制数字的格式化,只能用于整数。
更多信息请阅读https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling。
英文:
Use %1$s instead of %1$d.
d formats a number in decimal digits and can only be used with integers
For further information read https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论