2010-11-26 48 views
11

我必须在段落文本中嵌入可点击的文本,clickablespan可以做到这一点。但是,而不是使用默认的焦点/按/无焦点风格,我可以改变这些风格吗?就像聚焦时一样,背景颜色为蓝色,文字颜色为白色,未聚焦时背景颜色为白色,文字颜色为蓝色。可以更改clickablespan的外观和感觉

回答

20

下面是示例

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

的一点是,颜色跨度是可点击的跨度之后。否则,颜色不变。

+1

风格类的列表可以在这里找到: http://developer.android.com/reference/android/text/style/CharacterStyle。 HTML – clocksmith 2014-02-25 18:18:59

14

您可以覆盖的ClickableSpanupdateDrawState方法:

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