确认文本中存在注释,注释的格式和位置是否正确。
确认使用的文本注释库是否正确导入,并在使用注释时正确引用。
如果是自定义的注释库,需要检查注释识别方法是否正确,可以参考以下示例:
class MyAnnotationActivity : AppCompatActivity(), OnTextClickListener {
private var textView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_annotation)
textView = findViewById(R.id.textView)
val spannableString = SpannableString(getString(R.string.sample_text))
val startIndex = spannableString.indexOf("@")
val endIndex = spannableString.lastIndexOf("@") + 1
spannableString.setSpan(
TextAnnotationSpan(Color.BLUE),
startIndex,
endIndex,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
textView?.text = spannableString
textView?.movementMethod = LinkMovementMethod.getInstance()
textView?.highlightColor = Color.TRANSPARENT
textView?.setOnTextClickListener(this)
}
override fun onTextClick(widget: View, annotation: Annotation) {
if (annotation is TextAnnotation) {
Toast.makeText(this, annotation.text, Toast.LENGTH_SHORT).show()
}
}
}
TextAnnotationSpan是自定义的注释样式,TextAnnotation是自定义的注释类。
如果还不能解决问题,可以尝试使用其他第三方注释库或者手动实现注释。
上一篇:Android文本中的dp或sp