如何获得Spannable和TextView的颜色写单元测试如何获得、单元测试、颜色、TextView

2023-09-06 14:32:44 作者:差一点╯成帅哥

private void createStringEndingInRedColor(TextView tv, String word1, String word2) {
    Spannable word = new SpannableString(word1);
    tv.setText(word);

    Spannable wordTwo = new SpannableString(word2);

    wordTwo.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(Color.RED)), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.append(wordTwo);
}

我试图写的TextView的电视单元测试(使用Robolectric),以确保wordTwo是Color.RED。不过,我只需要TextView的电视的参考。一个人如何去了解这样的任务?

I'm trying to write a unit test (using Robolectric) for the TextView tv to ensure that wordTwo is Color.RED. However, I only have a reference to TextView tv. How does one go about such a task?

推荐答案

您可以在 Spannable 的TextView 使用 getSpans()

ForegroundColorSpan[] colorSpans = ((SpannableStringBuilder)textView.getText()).getSpans(0, textView.getText().length(), ForegroundColorSpan.class);
assertTrue(colorSpans[0].getForegroundColor() == Color.RED)