我随机设置了TextView的颜色。我有数组String[]colors={"蓝色"、"红色"、"绿色"、"黑色"、"黄色"、"灰色"}。我需要检查我的TextView在这个数组中的颜色并返回索引。我该怎么做?
首先从TextView
中获取文本(或背景(颜色。然后简单地迭代颜色列表(而不是颜色名称(,并在找到匹配项时返回索引。
当您使用List
而不是数组时,indexOf()
函数已经为您完成了此操作。
int textColor = textView.getCurrentTextColor();
List<Integer> colors = Arrays.asList(Color.BLUE, Color.RED, Color.GREEN, Color.BLACK, Color.YELLOW, Color.GRAY);
int index = colors.indexOf(textColor);