简书链接:代码设置按下和没按下color
文章字数:15,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public void setSelectorColor(TextView radioButton, int normal, int checked) {
int[] colors = new int[]{normal, checked, normal};
int[][] states = new int[3][];
states[0] = new int[]{-android.R.attr.state_pressed};
states[1] = new int[]{android.R.attr.state_pressed};
states[2] = new int[]{};
ColorStateList colorStateList = new ColorStateList(states, colors);
radioButton.setTextColor(colorStateList);
}
public void setSelectorDrawable(TextView cbButton, Drawable drawableNormal, Drawable drawableSelect) {
StateListDrawable drawable = new StateListDrawable();
//选中
drawable.addState(new int[]{android.R.attr.state_pressed}, drawableSelect);
//未选中
drawable.addState(new int[]{-android.R.attr.state_pressed}, drawableNormal);
cbButton.setBackgroundDrawable(drawable);
|