Recently in the review of the need to achieve this effect on-line Baidu, is to customize a class to inherit 4ClickableSpan, and then set in the Updatedrawstate method whether the underscore is false, But looked at the online implementation of the method is directly in the new class OnClick method to implement the onclick operation, the feeling is not easy to expand the use, so I customized an interface, through the interface to implement the external code calls, the specific code is as follows:
public class Nolineclickablespan extends Clickablespan{private Ionnolinetextclick ionnolinetextclick;public Nolineclickablespan () {super ();} @Overridepublic void Updatedrawstate (Textpaint ds) {ds.setcolor (Ds.linkcolor);d S.setunderlinetext (false);} @Overridepublic void OnClick (View widget) {Ionnolinetextclick.onclick ();} public void Setonnolinetextclick (Ionnolinetextclick i) {ionnolinetextclick = i;} public interface ionnolinetextclick{public void OnClick ();}}
XML file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:id=" @+id/tv_test " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:text= "Xiao Xi reply to the great East: You are a big and great good person"/></relativelayout>
The mainactivity calling code is as follows:
public class Mainactivity extends Activity {private TextView tv_test; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initview (); } private void Initview () { tv_test = (TextView) Findviewbyid (r.id.tv_test); Tv_test.setmovementmethod (Linkmovementmethod.getinstance ()); spannablestring spanstring = new Spannablestring (Tv_test.gettext (). toString ()); Nolineclickablespan span = new Nolineclickablespan (); Span.setonnolinetextclick (New Ionnolinetextclick () {@Overridepublic void OnClick () {System.out.println ("click User");}); Spanstring.setspan (span, 0, 2, spannablestring.span_exclusive_exclusive); Tv_test.settext (spanstring);}}
Among them Spanstring.setspan (span,0,2,spannablestring.span_exclusive_exclusize); 0,2 represents the beginning and end of the hyperlink font, I take 2 here, when I click on the west, the result is as follows:
Android Custom no underline clickablesapn hyperlink Text style