如何在TextView的文本上设置多个跨度(可单击和粗体)

时间:2016-08-10 13:29:25

标签: android spannablestring

    mSpannableString = new SpannableString("12:00PM");

    mSpannableString.setSpan(clickableSpan, 0, 7, 0);
    mSpannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);

    TextView textView = new TextView(getContext());
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(mSpannableString);

    mCancelFrom.setText(getString(R.string.cancel_from_text) + " " + mSpannableString);

我需要将文本设置为粗体并使其可单击。上面的代码我写在一个对话框中。字符串" 12:00 PM"正在显示。但它没有大胆的效果,也无法点击。你能用这个帮我吗?

1 个答案:

答案 0 :(得分:4)

试试这个,它可能对您有所帮助

SpannableString ss = new SpannableString("12:00PM");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            startActivity(new Intent(SendSMS.this, SendSMS.class));
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    };
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    mCancelFrom.setText(getString(R.string.cancel_from_text) + " " + ss);