android: Set link with in TextView

2019-01-23 04:25发布

I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this, which shows the same way, but I couldn't produce the expected results.

I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));

/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);                 
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);

In my output I see "Google" and no link. I also tried Clean project & building it again, but no success. I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.

What is lacking in my code to get the output ? BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2

Any help is highly appreciated.

5条回答
贼婆χ
2楼-- · 2019-01-23 04:50

I was also facing the same issue I resolved using following

String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));

for changing the link color to blue use

link.setLinkTextColor(Color.BLUE);
查看更多
Rolldiameter
3楼-- · 2019-01-23 04:50

Here is my 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));
}
查看更多
虎瘦雄心在
4楼-- · 2019-01-23 04:53
txtview.setMovementMethod(LinkMovementMethod.getInstance());

Pass this statement to your textview, and in string.xml set an string as

<string name="txtCredits"> <a href="http://www.google.com"></a></string>

Now pass this string name " android:text="@string/txtCredits" to your xml class where the txtview is there .

查看更多
甜甜的少女心
5楼-- · 2019-01-23 04:54

I finally got it working using the following code :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("<a href=\""+ l.getRightString() + "\">" + l.getLeftString() + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);

l.getRightString() - contains a url like http:\www.google.com l.getLeftString() - contains text for the url like "Go to Google"

RESULTS : Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.

Hope this helps.

查看更多
唯我独甜
6楼-- · 2019-01-23 05:04

Save your html in a string

<string name="link">&lt;a href="http://www.google.com">Google&lt;/a></string>

Set textview ID to to

textViewLinkable

In main activity use following code:

((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
查看更多
登录 后发表回答