2015-08-27 73 views
2

我想更改链接(textview)的默认颜色。如何更改android中可点击文本视图的颜色

SpannableString ss = new SpannableString("By continuing,I agree to HCP User Agreement and Terms of Services "); 
    ClickableSpan clickableSpan = new ClickableSpan() { 
     @Override 
     public void onClick(View textView) { 
      startActivity(new Intent(UserRegister.this, ForgotPassword.class)); 
     } 
    }; 
    ss.setSpan(clickableSpan, 48, 65, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    TextView textView = (TextView) findViewById(R.id.termsAndConditions); 
    textView.setText(ss); 
    textView.setMovementMethod(LinkMovementMethod.getInstance()); 
+0

'ss.setSpan(new ForegroundColorSpan(Color.RED),48,65,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)' – Raghunandan

+0

我有他们使用wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE),15,30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);但那么我应该在哪里通过clickablespan对象 – Loren

+0

http://stackoverflow.com/questions/15851655/android-change-the-background-color-of-a-clickablespan-when-clicked –

回答

0

如果你想改变整个TextView的文本颜色只需调用textView.setTextColor(R.color.text_color)

如果你想上色的TextView的文本只有部分:

final SpannableStringBuilder stringBuilder = new SpannableStringBuilder("your-string"); 
final int start = 0, end = textView.getText().length(); // Change to the start/end of the part to colorize 

// Apply some text-color 
stringBuilder.setSpan(new ForegroundColorSpan(getResources() 
    .getColor(R.color.text_color)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

// Apply changes to TextView 
textView.setText(stringBuilder); 

注意:使用字符串资源,不通过原始字符串("By continuing,I agree to HCP User Agreement and Terms of Services " - >更改为类似getString(R.string.accept_user_agreement)

相关问题