2010-11-26 4 views
11

Je dois incorporer un texte cliquable dans un texte de paragraphe, clickablespan peut le faire. Mais au lieu d'utiliser le style focus/press/unfocus par défaut, est-ce que je peux changer ce style? Comme lors de la mise au point, la couleur d'arrière-plan est bleue, la couleur du texte est blanche, la mise au point est annulée, la couleur d'arrière-plan est blanche et la couleur du texte est bleue.puis-je changer l'aspect et la convivialité du clickablespan

Répondre

20

Voici l'exemple

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

Le point est, couleur portée est après la cliquable portée. sinon, la couleur n'a pas changé.

+1

Une liste des classes de style se trouve ici: http://developer.android.com/reference/android/text/style/CharacterStyle. html – clocksmith

14

Vous pouvez remplacer la méthode updateDrawState de ClickableSpan:

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