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)
上一篇:Android的6:不能共享文件了吗?文件、Android
下一篇:的onActivityResult()和:未能交付结果resultinfo谁= NULL请求= 1的结果= -1数据=意图(有临时演员)结果、意图、演员、数据