Android数据绑定中的textView.setMovementMethod

问题描述 投票:0回答:1

我想在textview中实现可点击链接。我现在将我的所有UI迁移到Android数据绑定,并想知道如何实现它。

textView.setMovementMethod。

有什么建议?

最好,

SK

android-databinding clickable spannablestring
1个回答
0
投票

我发现了一种方法。这里是

创建一个静态方法,并在一个单独的类中添加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);

如果您知道更好的方法,请告诉我。谢谢 !

© www.soinside.com 2019 - 2024. All rights reserved.