控件 -- TextView-- 设置部分字体颜色、下划线、点击响应

SpannableStringBuilder

例子:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testMethod2();
    }

    private void testMethod() { //使用H5标签的方式
        TextView tv = findViewById(R.id.tv);
        String originStr = "To continue, agree to the %1$sThird-Party Access Notice%2$s.";
        CharSequence formatStr = Html.fromHtml(String.format(originStr, "", "")); //使用H5标签设置部分字体加粗、下划线效果
        Log.d(TAG, "zwm, formatStr: " + formatStr);
        ClickableSpan clickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Log.d(TAG, "zwm, onClick");
            }
        };

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(formatStr);
        UnderlineSpan[] underline = spannableStringBuilder.getSpans(0, formatStr.length(), UnderlineSpan.class); //获取已设置下划线的字体
        int start = spannableStringBuilder.getSpanStart(underline[0]);
        int end = spannableStringBuilder.getSpanEnd(underline[0]);
        int flag = spannableStringBuilder.getSpanFlags(underline[0]);
        spannableStringBuilder.setSpan(clickableSpan, start, end, flag); //设置下划线字体点击响应
        tv.setText(spannableStringBuilder);
        tv.setLinkTextColor(Color.BLUE); //设置下划线字体颜色
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void testMethod2() { //不使用H5标签的方式
        TextView tv = findViewById(R.id.tv);
        String originStr = "To continue, agree to the %1$sThird-Party Access Notice%2$s.";
        int start = originStr.indexOf("%1$s");
        String formatStr = originStr.replace("%1$s", "");
        int end = formatStr.indexOf("%2$s");
        formatStr = formatStr.replace("%2$s", "");
        Log.d(TAG, "zwm, formatStr: " + formatStr);
        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
        ClickableSpan clickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Log.d(TAG, "zwm, onClick");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.BLUE); //设置部分字体颜色
                ds.setUnderlineText(true); //设置部分字体下划线效果
            }
        };

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(formatStr);
        spannableStringBuilder.setSpan(styleSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); //设置部分字体加粗效果
        spannableStringBuilder.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); //设置部分字体点击响应
        tv.setText(spannableStringBuilder);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

你可能感兴趣的:(控件 -- TextView-- 设置部分字体颜色、下划线、点击响应)