2010-11-26 11 views
11

Tengo que incrustar un texto seleccionable en un texto de párrafo, clickablespan puede hacerlo. Pero en lugar de usar el estilo predeterminado de enfoque/presionar/desenfocar, ¿puedo cambiar ese estilo? Al igual que cuando se enfoca, el color de fondo es azul, el color del texto es blanco, cuando se desenfoca, el color de fondo es blanco, el color del texto es azul.puedo cambiar la apariencia de clickablespan

Respuesta

20

Aquí es el ejemplo

bottomMsg = (TextView) findViewById(R.id.bottom_msg); 
int start = bottomMsg.getText().toString().indexOf(terms); 
MovementMethod movementMethod = LinkMovementMethod.getInstance(); 
bottomMsg.setMovementMethod(movementMethod); 

Spannable text = (Spannable) bottomMsg.getText(); 
//TermAndConditions is a clickableSpan. 
text.setSpan(new TermAndConditions(), start, start + terms.length(), Spannable.SPAN_POINT_MARK); 
text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.customYellowColor)), start, start + terms.length(), 0); 

El punto es, color de envergadura es después de que el se puede hacer clic-palmo. de lo contrario, el color no cambia.

+1

una lista de clases de estilo se puede encontrar aquí: http://developer.android.com/reference/android/text/style/CharacterStyle. html – clocksmith

14

se puede anular el método de updateDrawStateClickableSpan:

SpannableString spannableString = new SpannableString("text"); 
spannableString.setSpan(
    new ClickableSpan() { 
     @Override 
     public void onClick(View widget) { 
      Toast.makeText(getContext(), "Click!", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void updateDrawState(TextPaint ds) { 
      ds.setColor(getResources().getColor(R.color.link)); 
     } 
    }, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 

textView.setText(spannableString); 

textView.setMovementMethod(LinkMovementMethod.getInstance()); 
Cuestiones relacionadas