button继承于textView,textView里有setTextColor方法,贪于方便就用.setTextColor(int color);这个方法。但是运行发现根本不起作用,文字都变成黑色的了。
看api发现还有setTextColor(ColorStateList colors)这个方法,传入ColorStateList对象。
ColorStateList whiteColor=getResources().getColorStateList(R.color.white);
currentButton.setTextColor(whiteColor);再次运行,发现起效果了!另外Button可以存在多种状态(pressed、focused或other),如果使用ColorStateList,你就能为它的每个状态提供不同的颜色。当然,这个是可以在xml里定义的!