2010-12-14 40 views

Répondre

10

Vous pouvez le faire de cette façon;

mTextView = (TextView) findViewById(R.id.textView); 
String text = "Visit my developer.android.com"; 
mTextView.setText(text); 
// pattern we want to match and turn into a clickable link 
Pattern pattern = Pattern.compile("developer.android.com"); 
// prefix our pattern with http:// 
Linkify.addLinks(mTextView, pattern, "http://") 

Espérons que cela aide. S'il vous plaît voir ce blog post pour plus de détails. (Ce n'est pas à moi, et je n'y suis pas associé de toute façon.) Publié ici à titre d'information seulement).

141

Essayez cette

txtTest.setText(Html.fromHtml("<a href=\"http://www.google.com\">Google</a>")); 
txtTest.setMovementMethod(LinkMovementMethod.getInstance()); 

Rappelez-vous: ne pas utiliser Android: autoLink = attribut "web" avec elle. car cela provoque LinkMovementMethod ne fonctionne pas.

Mise à jour pour le SDK 24+ La fonction Html.fromHtml dépréciée sur Android N (SDK v24), donc tourner à utiliser cette méthode:

String html = "<a href=\"http://www.google.com\">Google</a>"; 
    Spanned result; 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { 
     result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY); 
    } else { 
     result = Html.fromHtml(html); 
    } 
    txtTest.setText(result) 
    txtTest. setMovementMethod(LinkMovementMethod.getInstance()); 

Voici la liste des drapeaux:

FROM_HTML_MODE_COMPACT = 63; 
FROM_HTML_MODE_LEGACY = 0; 
FROM_HTML_OPTION_USE_CSS_COLORS = 256; 
FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32; 
FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16; 
FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2; 
FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8; 
FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4; 
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1; 
+3

+1 fonctionne bien et est facile à mettre en œuvre! – Matthias

+11

Rappelez-vous de ne pas utiliser android: autoLink = attribut "web". car cela provoque LinkMovementMethod ne fonctionne pas. –

+1

Nice, rapide et simple ... + 1 –