将颜色更改为字符串中的特定文本

时间:2018-08-29 14:55:32

标签: android xml android-layout

我有一个字符串,在其中我使用了一个垃圾邮件类,给文本添加颜色,但是我没有得到结果,我进行了研究,但没有任何解决方法

HTML
<input type="number" id='price' onblur='checkMinPrice("idOfItem")'>

JS
function checkMinPrice(idOfItem){

var enteredPrice = $("#price").val();
// use ajax here to fetch minimum price from server.
// suppose we have fetched price and it is saved in "MinPrice" variable

if(MinPrice >= enteredPrice){
    //give alert or any other type of error here.
    alert("Entered price must be greater than minimum price");
    $("#price").focus(); // move the curson to price field to ask him to enter higher price.
}
}

我在这里使用垃圾邮件类

    <string name="successfull_registration">
    Please, use the means of 
    payment of the Credit Bank that most suits you. Box Office, 
    Transfer, Cashier or BCP Agent, to make  payment to account
    <span class="blue">0102-0228345543469</span> Pympack SAC \n
    </string>

2 个答案:

答案 0 :(得分:2)

改用SpannableString。

val string = SpannableString(resources.getString(R.string.successfull_registration))
string.setSpan(
    ForegroundColorSpan(Color.BLUE),
    144,
    162,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
  )

textView.text = string

答案 1 :(得分:1)

如果要使用跨度,可以在java中进行配置:

        SpannableString Spann = new SpannableString ( "your text source" );
        ClickableSpan CSpan = new ClickableSpan (){
            @Override
            public void onClick(View v){
    Toast.makeText(getApplicationContext() , "your clickable",
      Toast.LENGTH_LONG).show();
            }
        };
        
        Spann.setSpan(backgroundSpan, 0, Spann.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        yourTextViewId.setText(Spann);
        yourTextViewId.setMovementMethod(LinkMovementMethod.getInstance());