java [TextView Clickable] #tags:Android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [TextView Clickable] #tags:Android相关的知识,希望对你有一定的参考价值。

//how-to-set-the-part-of-the-text-view-is-clickable

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

//to-draw-an-underline-below-the-textview-in-android
//1st Approach
//For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

//2nd Approach You can make use of setPaintFlags method of TextView to underline the text of TextView.
//For eg.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");
You can refer constants of Paint class if you want to strike thru the text.

//3rd Approach

//Make use of Html.fromHtml(htmlString);

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

//OR

txtView.setText(Html.fromHtml("<u>underlined</u> text"));

以上是关于java [TextView Clickable] #tags:Android的主要内容,如果未能解决你的问题,请参考以下文章

解决点击span元素报错Element is not clickable at point

`focusable` 和 `clickable` 之间的区别

Android的clickable与事件分发关系详解

Android的clickable与事件分发关系详解

echart坐标轴文本axisLabel.clickable,支持点击事件响应

PHP make_clickable($文本)