如何设置部分文本视图可点击 [英] How to set the part of the text view is clickable

查看:21
本文介绍了如何设置部分文本视图可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本Android 是一个软件堆栈".在此文本中,我想设置stack"文本是可点击的.从某种意义上说,如果您单击它将重定向到新活动(而不是在浏览器中).

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).

我试过了,但我没有.

推荐答案

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 
  ...
  android:textColorLink="@drawable/your_selector"
/>

这篇关于如何设置部分文本视图可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆