【问题标题】:how to change colour of clickable textview in android如何在android中更改可点击文本视图的颜色
【发布时间】:2015-08-27 15:19:18
【问题描述】:

我想更改链接(文本视图)的默认颜色。

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

【问题讨论】:

  • ss.setSpan(new ForegroundColorSpan(Color.RED),48,65,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  • 我有他们使用 wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);但是我应该在哪里传递 clickablespan 对象
  • @Raghunandan 那么我应该在哪里传递 ClickableSpan 的对象

标签: android textview


【解决方案1】:

如果您想更改整个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) 之类的东西)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多