I want text view as a clickable link

2019-01-22 20:17发布

<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Forget password?"
    android:autoLink="all"
    android:linksClickable="true"
    android:textColor="@color/lgreen"
    android:textStyle="italic"
     />

Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me how can i do that as a clickable and move to another link. i tried more times by seeing examples. but i can't. Can some one explain me clearly how to change text view as a clickable link.

8条回答
唯我独甜
2楼-- · 2019-01-22 20:48

You can add click listener to TextView and start websearch:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view)
    {
         Intent browser= new Intent(Intent.ACTION_VIEW, Uri.parse(PASTE_YOUR_URL_HERE));  
         startActivity(browser);  
    }

});

and xml looks like:

<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/link_string"  
    android:textSize="14dp"  
    android:autoLink="web" /> 
查看更多
Rolldiameter
3楼-- · 2019-01-22 20:48

this is the EDITED answer

Hi you can try by replacing this in you layout.xml file

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="visit the site at www.google.com from here"
    android:autoLink="all"
    android:clickable="true"
    android:linksClickable="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

I have tried it and it will surely work. and treats "www.google.com" as web link

查看更多
Explosion°爆炸
4楼-- · 2019-01-22 20:49

In the string file put this code

<string name="link">'<a href="http://www.www.com" >ForGet Password</a>'</string> 

and in the XML file

android:text="@string/link"
查看更多
ら.Afraid
5楼-- · 2019-01-22 20:50

In your onCreate() method put this code:

TextView clickableTextLink = (TextView)findViewById(R.id.your_textview_id); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance());

I tried a number of solutions. But this worked perfectly for me.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-22 20:56

Just add onClick tag to your TextView which equals the defined function. Just as below

<TextView
  android:id="@+id/link"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="Forget password?"
  android:autoLink="all"
  android:linksClickable="true"
  android:textColor="@color/lgreen"
  android:textStyle="italic"
  android:onClick="callFunction"/>
查看更多
做自己的国王
7楼-- · 2019-01-22 20:58

Here is simple implementation tested up to Android N

    String termsText = "By registering, you are agree to our";
    String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
    String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
    String allText = termsText + termsLink + privacyLink;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
    } else {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
    }
查看更多
登录 后发表回答