如何重置按钮颜色?

时间:2013-03-25 20:44:12

标签: java android

我有一个游戏,有16个按钮。点击每个按钮后,我为按钮设置了一种颜色。现在,点击16次后,我的活动重新加载。我想重置所有按钮的颜色,以便在重新加载后启动值。我的按钮有一些透明度,在buttons.xml中设置。这是我的代码,我计算按钮点击次数,其中16次我重新加载活动。如何重置所有按钮的颜色,但是要从buttons.xml维护透明度和设置?

final OnClickListener clickListener = new OnClickListener() {

            private Button buttonClicked;
            private int counter = 0;
            private int numberOfClicks = 0;

            public void onClick(View v) {
                numberOfClicks++;
                if (brojacKlikova < 10) {

                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));

                if (buttonClicked == null) {
                    // first button is clicked
                    buttonClicked = button;
                } else {
                    // second button is clicked
                    if (buttonClicked.getTag().equals(button.getTag())) {
                        Toast.makeText(Spojnice.this, "Correct", Toast.LENGTH_SHORT).show();
                        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.setEnabled(false);
                        button.setEnabled(false);
                        counter = counter + 5;
                        score.setText("Points: " + counter);
                    } else {
                        Toast.makeText(Spojnice.this, "Wrong!", Toast.LENGTH_SHORT).show();
                        buttonClicked.setEnabled(false);
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
                        button.getBackground().clearColorFilter();
                    }
                    // remember to reset background of button and buttonClicked before
                    buttonClicked = null;
                }
                }else{

                nextQuestion();

         }
            }
     };

如果需要,这里是buttons.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>

            <stroke
                android:width="2dp"
                android:color="#254117"/>

            <corners
                android:radius="4dp"/>

            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"/>
            <solid android:color="#98800000" />


        </shape>
    </item>
        <item>
        <shape>

            <stroke
                android:width="2dp"
                android:color="#254117"/>

            <corners
                android:radius="4dp"/>

            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"/>
            <solid android:color="#60000000" />


        </shape>
    </item>
 </selector>

0 个答案:

没有答案