我想在textview中实现可点击链接。我现在将我的所有UI迁移到Android数据绑定,并想知道如何实现它。
textView.setMovementMethod。
有什么建议?
最好,
SK
我发现了一种方法。这里是
创建一个静态方法,并在一个单独的类中添加BindingAdapter注释
@BindingAdapter("app:specialtext")
public static void setSpecialText(TextView textView, SpecialText specialText) {
SpannableString ss = new SpannableString(specialText.getText());
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.d("tag", "onSpecialClick: ");
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
ss.setSpan(clickableSpan, specialText.getStartIndex(), ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(ss);
}
在绑定布局文件中
<variable
name="specialText"
type="com.example.test.data.SpecialText" />
<TextView
android:id="@+id/st_textView"
app:specialtext="@{specialText}"/>
在您使用布局的活动或片段中
SpecialText specialText = new TCText();
specialText.setStartIndex(15);
specialText.setText(getActivity().getString(R.string.special_text));
binding.setSpecialText(tcText);
如果您知道更好的方法,请告诉我。谢谢 !