如何设置文本视图的一部分是可以点击视图、如何设置、文本

2023-09-11 20:27:40 作者:不能给我未来就别毁我现在

我的文字 Android是一个软件堆栈。在本文中,我想将堆栈的文字是可以点击的。在这个意义上,如果你点击它将重定向到新的活动(而不是在浏览器中)。

I have the text "Android is a Software stack". In this text i want to set the "stack" text is clickable. in the sense if you click on that it will redirected to a new activity(not in the browser).

我试过,但我没有得到。

I tried but i am not getting.

推荐答案

android.text.style.ClickableSpan 能解决你的问题。

SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        startActivity(new Intent(MyActivity.this, NextActivity.class));
    }
    @Override
    public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

//在XML:TextView的:机器人:textColorLink =@可绘制/ your_selector

//In XML: TextView: android:textColorLink="@drawable/your_selector"