0

我想在字符串“confirm_msg”中添加金额

    final int colorGreen = getResources().getColor(R.color.green_dark);
    SpannableString spannable = new SpannableString("$" +amount);
    spannable.setSpan(new ForegroundColorSpan(colorGreen), 0, ("$" +   amount).length(), 0);
    txtConfirmMSG.setText(getString(R.string.confirm_msg, spannable));

这是 xml 中的字符串:

    <string name="confirm_msg">PLEASE CONFIRM THAT YOU WISH TO SEND A  %1$s </string>
4

2 回答 2

2
// try this way

<string name="confirm_msg">PLEASE CONFIRM THAT YOU WISH TO SEND A  %1$d$ </string>

 int amount = 150000;
        txtConfirmMSG.setText(setSpanColor(String.format(getString(R.string.confirm_msg),amount),String.valueOf(amount)));

 private SpannableStringBuilder setSpanColor(
            final String str,final String text) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(str);

        if (str.contains(text)) {
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(getResources().getColor(R.color.green_dark));
                }
            }, str.indexOf(text), str.indexOf(text)
                    + text.length(), 0);
        }
        return ssb;

    }
于 2013-10-03T05:30:38.240 回答
0

最好使用 HTML 在Spannable.

您可以检查该答案:Highlighting Text Color using Html.fromHtml() in Android?

于 2013-10-03T05:04:22.080 回答