不能施放到android.graphics.drawable.colordrawable



我遇到了一个问题,试图从文本视图中获取背景颜色并将其设置为按钮的背景。

我该如何解决这个问题?为什么我的条件不正确?

我的代码是:

    public void randomBotaoCorrecto() {
            Random generator = new Random();
            int rand = generator.nextInt(3);
            TextView textElement = (TextView) findViewById(R.id.TVnomes);
            int cor = textElement.getCurrentTextColor();
            int cob1, cob2, cob3;
            Button button11 = (Button) findViewById(R.id.button1);
            Button button22 = (Button) findViewById(R.id.button2);
            Button button33 = (Button) findViewById(R.id.button3);
            ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
            ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
            ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();
            switch (rand) {
                case 0:
                    button11.setBackgroundColor(cor);
                    do {
                        randomBotoes(button22);
                        cob2 = buttonColor2.getColor();
                    } while (cor == cob2);
                    do {
                        randomBotoes(button33);
                        cob3 = buttonColor3.getColor();
                    } while (cor == cob3 && cob3 == cob2);
                    break;
                case 1:
                    button22.setBackgroundColor(cor);
                    do {
                        randomBotoes(button11);
                        cob1 = buttonColor1.getColor();
                    } while (cob1 == cor);
                    do {
                        randomBotoes(button33);
                        cob3 = buttonColor3.getColor();
                    } while (cor == cob3 && cob3 == cob1);
                    break;
                case 2:
                    button33.setBackgroundColor(cor);
                    do {
                        randomBotoes(button22);
                        cob2 = buttonColor2.getColor();
                    } while (cor == cob2);
                    do {
                        randomBotoes(button11);
                        cob1 = buttonColor1.getColor();
                    } while (cor == cob1 && cob1 == cob2);
                    break;
            }
        }

我得到的结果是:

 Caused by: java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable

所以,现在我有:

public void randomBotaoCorrecto() {
    Random generator = new Random();
    int rand = generator.nextInt(3);
    TextView textElement = (TextView) findViewById(R.id.TVnomes);
    Drawable cor = textElement.getBackground();
    int colorb1, colorb2, colorb3;
    Button button11 = (Button) findViewById(R.id.button1);
    Button button22 = (Button) findViewById(R.id.button2);
    Button button33 = (Button) findViewById(R.id.button3);
    ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
    ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
    ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();
    switch (rand) {
        case 0:
            button11.setBackground(cor);
            colorb1 = buttonColor1.getColor();
            do {
                randomBotoes(button33);
                randomBotoes(button22);
                colorb2 = buttonColor2.getColor();
                colorb3 = buttonColor3.getColor();
            } while (colorb1 == colorb3 && colorb3 == colorb2  && colorb1  == colorb3 );
            break;
        case 1:
            button22.setBackground(cor);
            colorb2 = buttonColor2.getColor();
            do {
                randomBotoes(button11);
                randomBotoes(button33);
                colorb1 = buttonColor1.getColor();
                colorb3 = buttonColor3.getColor();
            } while (colorb2 == colorb3 && colorb3 == colorb1 && colorb1 == colorb2);
            break;
        case 2:
            button33.setBackground(cor);
            colorb3 = buttonColor3.getColor();
           do {
                randomBotoes(button11);
                randomBotoes(button22);
               colorb2 = buttonColor2.getColor();
               colorb1 = buttonColor1.getColor();
            } while (colorb3 == colorb1 && colorb1 == colorb2 && colorb3 == colorb2);
            break;
    }
}

但仍然相同的结果

要获取文本视图的背景,您应该调用

Drawable cor = textElement.getBackground();
YourButton.setBackground(cor);

然后将该值设置为您的按钮。

注意,以这种方式设置背景需要API 16。