Quiero crear un vínculo para un texto de vista de texto 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 correspondiente. Cualquier idea es bienvenida.
80

Respuestas:
Prueba esto y cuéntame qué sucedió.
Usando código java:
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));Desde el nivel de API> = 24 en adelante
Html.fromHtml(String source)está obsoleto en su lugarfromHtml(String, int), use ,textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));O en el archivo XML de diseño, dentro de los atributos del widget TextView
android:autoLink="web" android:linksClickable="true"fuente
android:autoLink="web"de XML. Tenía ambos y no funcionó.utilizar
android:autoLink="web"en el xml de su TextView. Debería convertir automáticamente las URL en las que se puede hacer clic (si se encuentran en el texto)fuente
Todo probado y funcionando al 100%
Solución: a
android:autoLink="web"continuación se
muestra un ejemplo completo de diseño de muestra XML
<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" />Cadena en string.xml
<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</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>fuente
Esto también se puede hacer usando la propiedad predeterminada de Textview
android:autoLink="email"fuente
Nota: - Html.fromHtml está obsoleto en Android N
Necesita verificar y soporte
Android Ny versiones superiores 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
No puede querer agregar mediante programación la marca autoLink en TextView.
De esta manera no es necesario agregar
<a href='somelink'>etiquetas.Lo cual es una desventaja, si desea agregar
hyperlinkun,textno puede hacerlo de esta manera. por ejemplo, no puedes 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 de ambos enfoques: -
https://github.com/hiteshsahu
fuente
Para la última versión de SDK
fromHtmlestá en desuso Use debajo de la líneaString 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)); }fuente
He creado una función de extensión siguiente de una vista de texto.
@SuppressLint("SetTextI18n") fun TextView.makeHyperLink(url: String) { text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT) } else { Html.fromHtml("<a href='${url}'>${text}</a>") } movementMethod = LinkMovementMethod.getInstance() isClickable = true }y llamándolo así
tvTos.makeHyperLink(tos_url)También puede pasar texto como parámetro.
fuente