欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

代码设置按下和没按下color

程序员文章站 2024-03-18 13:01:10
...
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);