2010-12-14 45 views

Respuesta

10

Puede hacerlo de esta manera;

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://") 

Espero, esto ayuda. Por favor, consulte este blog post para más detalles. (No es mío, y no estoy asociado con él de todos modos. Publicado aquí solo con fines informativos).

141

probar este

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

Recuerde: no utilice atributo android: autoLink = "web" con ella. porque causa que LinkMovementMethod no funcione.

Actualización para SDK 24+ La función Html.fromHtml obsoleta en Android N (v24 SDK), por lo que recurren a usar este método:

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()); 

Aquí está la lista de las banderas:

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 funciona bien y es fácil de implementar. – Matthias

+11

Recuerda no usar el atributo android: autoLink = "web". porque causa que LinkMovementMethod no funcione. –

+1

Agradable, rápido y simple ... + 1 –

Cuestiones relacionadas