Android Custom no underline clickablesapn hyperlink Text style

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.