将正则表达式 %1$d 转换为字符串在安卓中

huangapple go评论72阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月23日 15:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63544506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定