[英]Unresolved reference: setSpan in Kotlin
I want to set symbol * at mandatory field.我想在必填字段中设置符号 *。 for that i can use below code line:
为此,我可以使用以下代码行:
hint_mobile!!.setText(Html.fromHtml(resources.getString(R.string.mobile_number) + "<sup> * </sup>"));
it is work but i can't set the red color at this symbol *这是可行的,但我无法在此符号处设置红色 *
So i use another example like below :所以我使用如下另一个例子:
hint_mobile!!.setText(resources.getString(R.string.mobile_number))
val str = hint_mobile!!.text.toString()
val loc = hint_mobile!!.text.toString().indexOf("*")
str!!.setSpan(ForegroundColorSpan(Color.RED), loc, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
All code set grate but error come on below line:所有代码设置炉排但错误出现在以下行:
str!!.setSpan(ForegroundColorSpan(Color.RED), loc, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
ERROR错误
Unresolved reference: setSpan
So How to i resolve this error ??那么我该如何解决这个错误??
You need to use SpannableString, something like this:您需要使用 SpannableString,如下所示:
val spannableString = SpannableString("${resources.getString(R.string.mobile_number)} *")
val loc = spannableString.toString().indexOf("*")
spannableString.setSpan(ForegroundColorSpan(Color.RED), loc, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
hint_mobile!!.setText(spannableString.toString())
@Alex code work but there is you want to set the position where to show *
. @Alex 代码有效,但您想设置显示
*
的位置。 So I can use below code.所以我可以使用下面的代码。 It is show the symbol
*
at end of the String
.它在
String
末尾显示符号*
。
var simple : String? = resources.getString(R.string.mobile_number)
var colored : String? = " *"
var builder = SpannableStringBuilder()
builder.append(simple)
var start = builder.length
builder.append(colored)
var end = builder.length
builder.setSpan(ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
hint_mobile!!.setText(builder);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.