2010-11-26 49 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());