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

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

问题描述

我的文字 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"

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

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