example edittext and android textview

edittext - palabra pulsable dentro de TextView en Android



textview and button android example (6)

Tengo TextView con texto que cambió dinámicamente. Este texto contiene cadenas como <a href=''myWord''>myWord</a> . Quiero que después de hacer clic en este "enlace" myWord aparezca en el texto de edición en la misma actividad.

Este es mi código:

txt.setText(Html.fromHtml("...<a href=''link''>link</a>...")); txt.setMovementMethod(LinkMovementMethod.getInstance());

Funciona bien para las URL dentro del atributo href, pero hay un error para otro formato.

Encontré muchas preguntas similares en el StackOverflow pero todas fueron sobre enlaces url. En mi aplicación quiero crear "enlace" dentro de la actividad. En general, puedo cambiar la etiqueta a otra si es dependiente ...

¡Por favor, ayúdame! ¡Gracias!

----- RESUELTO ----- ¡Gracias Jacob Phillips por la idea!

Que sea interesante para alguien en el futuro. Este es un código:

//This is my string; String str = "<b>Text</b> which contains one <a href=''#''>link</a> and another <a href=''#''>link</a>"; //TextView; TextView txt = new TextView(this); //Split string to parts: String[] devFull = data[v.getId()][1].split("<a href=''#''>"); //Adding first part: txt.append(Html.fromHtml(devFull[0])); //Creating array for parts with links (they amount always will devFull.length-1): SpannableString[] link = new SpannableString[devFull.length-1]; //local vars: ClickableSpan[] cs = new ClickableSpan[devFull.length-1]; String linkWord; String[] devDevFull = new String[2]; for(int i=1; i<devFull.length; i++){ //obtaining ''clear'' link devDevFull = devFull[i].split("</a>"); link[i-1] = new SpannableString(devDevFull[0]); linkWord = devDevFull[0]; cs[i-1] = new ClickableSpan(){ private String w = linkWord; @Override public void onClick(View widget) { // here you can use w (linkWord) } }; link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0); txt.append(link[i-1]); try{ txt.append(Html.fromHtml(devDevFull[1])); } catch(Exception e){} }


Esto debería funcionar. Solo cambia el texto de tu edittext en OnClickListener . Puede reducirse pero esto debería funcionar.

private void foo() { SpannableString link = makeLinkSpan("click here", new View.OnClickListener() { @Override public void onClick(View v) { // respond to click } }); // We need a TextView instance. TextView tv = new TextView(context); // Set the TextView''s text tv.setText("To perform action, "); // Append the link we created above using a function defined below. tv.append(link); // Append a period (this will not be a link). tv.append("."); // This line makes the link clickable! makeLinksFocusable(tv); } /* * Methods used above. */ private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) { SpannableString link = new SpannableString(text); link.setSpan(new ClickableString(listener), 0, text.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE); return link; } private void makeLinksFocusable(TextView tv) { MovementMethod m = tv.getMovementMethod(); if ((m == null) || !(m instanceof LinkMovementMethod)) { if (tv.getLinksClickable()) { tv.setMovementMethod(LinkMovementMethod.getInstance()); } } } /* * ClickableString class */ private static class ClickableString extends ClickableSpan { private View.OnClickListener mListener; public ClickableString(View.OnClickListener listener) { mListener = listener; } @Override public void onClick(View v) { mListener.onClick(v); } }


La mejor solución que conozco es crear tu propia clase Button. Podría hacer que el botón tenga un fondo transparente para que el usuario solo vea el texto. Luego, cuando se presiona el botón, cambie el Color de texto y el Estilo de texto del botón para que tenga un color más oscuro y esté subrayado. Esto funcionará exactamente como lo hace un enlace. Luego puede usar startActivity para ir a la actividad apropiada. No debe utilizar hipervínculos para conectarse a otras actividades dentro de su aplicación.


Mejor enfoque es

SpannableString ss = new SpannableString("Android is a Software stack"); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { startActivity(new Intent(MyActivity.this, NextActivity.class)); } }; ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //where 22 and 27 are the starting and ending index of the String. Now word stack is clickable // onClicking stack it will open NextActiivty TextView textView = (TextView) findViewById(R.id.hello); textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance());


Mi opinión personal sería hacer una segunda vista de texto que contenga el texto que desea que sea su enlace. Entonces podrías hacer tu acción en el onClick de este segundo textView. Además, como se indicó anteriormente en zzzzzzzzzzz, puede elegir cambiar las propiedades de la fuente de ese texto a lo que quiera una vez que se haya hecho clic.


Para que sea una respuesta completa mezclando respuestas;

private void textAreaInit() { String str = "<a href=''#''>Link 1</a> and <a href=''#''>Link2</a> is here."; TextView tv = mConfirmText; String[] devFull = str.split("<a href=''#''>"); tv.append(Html.fromHtml(devFull[0])); SpannableString[] link = new SpannableString[devFull.length-1]; ClickableSpan[] cs = new ClickableSpan[devFull.length-1]; String linkWord; String[] devDevFull = new String[2]; for(int i=1; i<devFull.length; i++) { //obtaining ''clear'' link devDevFull = devFull[i].split("</a>"); link[i-1] = new SpannableString(devDevFull[0]); linkWord = devDevFull[0]; final String a = linkWord; cs[i-1] = new ClickableSpan() { private String w = a; @Override public void onClick(View widget) { if(w.equals("Link 1")) { Intent intent = new Intent(PrintPropertiesActivity.this, ViewerAcivity.class); intent.putExtra("title", "Link1"); intent.putExtra("uri", "link1"); intent.putExtra("type", "1"); startActivity(intent); } else { Intent intent = new Intent(PrintPropertiesActivity.this, ViewerAcivity.class); intent.putExtra("title", "Link2"); intent.putExtra("uri", "link2"); intent.putExtra("type", "2"); startActivity(intent); } } }; link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0); tv.append(link[i-1]); try{ tv.append(Html.fromHtml(devDevFull[1])); } catch(Exception e){} } makeLinksFocusable(tv); } private void makeLinksFocusable(TextView tv) { MovementMethod m = tv.getMovementMethod(); if ((m == null) || !(m instanceof LinkMovementMethod)) { if (tv.getLinksClickable()) { tv.setMovementMethod(LinkMovementMethod.getInstance()); } } }


Puede utilizar el código de abajo;

SpannableString myString = new SpannableString(Html.fromHtml("Please "+"<font color=/"#F15d36/"><u>"+"login"+"</u></font>" +" or "+ "<font color=/"#F15d36/"><u>"+"sign up"+ "</u></font>"+" to begin your YupIT experience")); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show(); } }; ClickableSpan clickableSpan1 = new ClickableSpan() { @Override public void onClick(View textView) { Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show(); } }; myString.setSpan(clickableSpan,6,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); myString.setSpan(clickableSpan1,15,23,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); myString.setSpan(new ForegroundColorSpan(Color.parseColor("#F15d36")),6, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); myString.setSpan(new ForegroundColorSpan(Color.parseColor("#F15d36")),15,23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tvFound.setMovementMethod(LinkMovementMethod.getInstance()); tvFound.setText(myString);