Android development tips -- remove the autolink underline in TextView and textviewautolink
We know that the autolink and its type of textview are set in the layout file. The color of the link is displayed on the textivew, and there is an underline in the text to indicate that you can click. When we click textview, the application will jump to the corresponding interface based on the type we set. But sometimes, we need to remove the underline on the interface as needed and keep the function unchanged.
If autolink is already set, it is invalid to set TextPaint to underline-free in textview. the source code of the ClickableSpan class inherited by URLSpan is as follows:
public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance { /** * Performs the click action associated with this span. */ public abstract void onClick(View widget); /** * Makes the text underlined and in the link color. */ @Override public void updateDrawState(TextPaint ds) { ds.setColor(ds.linkColor); ds.setUnderlineText(true); }}
It can be seen that it is re-set to underline here.
To solve this problem, you only need to set a Spannable object without any underscores.
First, inherit a CharacterStyle or its implemented subclass and override the updateDrawState method. The Code is as follows:
/** Date: 14-9-4 * Project: Access-Control-V2 */package cn. irains. access. v2.common; import android. text. textPaint; import android. text. style. underlineSpan;/*** underlined Span * Author: msdx (645079761@qq.com) * Time: 14-9-4 */public class NoUnderlineSpan extends UnderlineSpan {@ Override public void updateDrawState (TextPaint ds) {ds. setColor (ds. linkColor); ds. setUnderlineText (false );}}
After setting the content in textview, call the following code to set a span:
NoUnderlineSpan mNoUnderlineSpan = new NoUnderlineSpan();if (textview.getText() instanceof Spannable) { Spannable s = (Spannable) textview.getText(); s.setSpan(mNoUnderlineSpan, 0, s.length(), Spanned.SPAN_MARK_MARK);}
If you use it in listview and use holder, you can create a span object and reset the span object after calling setText. In this way, textview does not have any underline on the interface, but it retains the autolink function.
How to Set underline in android TextView
1. TextView TV = new TextView (this );
TV. getPaint (). setFlags (Paint. UNDERLINE_TEXT_FLAG); // underline
TV. setText ("use code to implement underline style ");
TV. setTextColor (Color. WHITE );
2. TV = new TextView (this );
TV. setText (Html. fromHtml ("<u> Use html to implement underline style </u> "));
How to underline the textView of Android
In the mall project, the original price and the price after the price reduction are required, and the underline textView is inevitable. getPaint (). setFlags (Paint. UNDERLINE_TEXT_FLAG); // underline textView. getPaint (). setAntiAlias (true); // anti-sawtooth textview. getPaint (). setFlags (Paint. STRIKE_THRU_TEXT_FLAG); // strip setFlags (Paint. STRIKE_THRU_TEXT_FLAG | Paint. ANTI_ALIAS_FLAG); // you can specify a hyphen (-) and add a clear textView. getPaint (). setFlags (0); // deselect the dashes.