2012-02-15 7 views
57

Quiero hacer un enlace para un texto textview como Google. ¿Hay alguna forma de hacer un enlace como este? (es decir) Al hacer clic en la palabra Google, debería abrir el enlace apropiado. Cualquier idea es bienvenida.Hacer una vista de texto de hipervínculo en android

+0

duplicado: http://stackoverflow.com/questions/2116162/how-to-display-html -en-textview – m0skit0

+0

http: // stackoverflow.com/questions/3204036/hyperlink-in-android –

+0

solo use Linkify.addLinks (TextView, Linkify.ALL); – Nepster

Respuesta

96

probar esto, y quiero saber lo que suceda ..

TextView textView =(TextView)findViewById(R.id.textView); 
textView.setClickable(true); 
textView.setMovementMethod(LinkMovementMethod.getInstance()); 
String text = "<a href='http://www.google.com'> Google </a>"; 
textView.setText(Html.fromHtml(text)); 
+0

obtuve el resultado esperado. Gracias por tu respuesta – Srinivas

+2

También se puede hacer mediante el uso de android: autoLink = "email" –

42

use android:autoLink="web" en el archivo XML de su TextView. Debe convertir automáticamente las URL de poder (si se encuentra en el texto)

+13

También incluye android: linksClickable = "true" –

+0

@waqaslam en este caso donde ponemos un enlace. –

+1

@ Garg's Es el texto que estableces en la vista de texto que puede contener hipervínculos – waqaslam

25

Todos probados y funcionando al 100%
Solución: android:autoLink="web"
a continuación es un ejemplo completo

Muestra LayoutXml

<TextView 
     android:id="@+id/txtLostpassword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:autoLink="email" 
     android:gravity="center" 
     android:padding="20px" 
     android:text="@string/lostpassword" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/txtLostpassword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:autoLink="web" 
     android:gravity="center" 
     android:padding="20px" 
     android:text="@string/defaultpassword" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

para cuerdas en string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</a></string> 

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string> 
+3

No funciona si cambio esta línea> http://www.cleverfinger.com.au/user-guide/ para un texto diferente como: haga clic aquí – Daniel

+0

que no funciona para mí. –

+2

esto funciona solo si el texto contiene una URL completa como: www.something.com –

5

Esto también se puede hacer mediante el uso de la propiedad predeterminada de Textview

android:autoLink="email" 
2

Nota: - Html.fromHtml está en desuso en Android N

Necesita comprobar y admitir Android N y versiones superiores iones de Android

    //Set clickable true 
       tagHeading.setClickable(true); 

        //Handlle click event 
        tagHeading.setMovementMethod(LinkMovementMethod.getInstance()); 

       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { 
        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY)); 
       } else { 
        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>")); 
       } 

Alternativamente

Usted puede no querer Identificación programación agregar el indicador de autoLink en TextView.

androide: autoLink = "web"

androide: linksClickable = "true"

De esta manera no es necesario añadir <a href='somelink'> etiquetas.

Cuál es una desventaja, si usted quiere agregar hyperlink en un text usted no puede hacerlo de esta manera. por ejemplo, no se puede hacer algo como esto: - [hiteshsahu] [1]

  <TextView 
       android:id="@+id/tag_info" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tag_ll" 
       android:layout_gravity="left" 
       android:layout_margin="10dp" 
       android:autoLink="web" 
       android:linksClickable="true" 
       android:text="https://github.com/hiteshsahu" 
       android:textColor="@color/secondary_text" /> 

El resultado tanto de enfoque: -

https://github.com/hiteshsahu

1

para la última versión del SDK fromHtml es obsoleto uso debajo de la línea

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>"; 
    if (Build.VERSION.SDK_INT >= 24) { 
     textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY)); 
    } else { 
     textView.setText(Html.fromHtml(yourtext)); 
    } 
Cuestiones relacionadas