简体   繁体   中英

How to change the background color of a button that wasn't clicked?

My goal here is every time a button is clicked the Button background colors go back to their default color and the Button that is clicked changes color. I'm hoping to do this in JAVA , I think a for loop is the way to go but I'm not sure how to edit Button that wasn't clicked.

Here is my XML

    <Button
    android:id="@+id/but1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:background="@drawable/background_selector"
    android:onClick="buttonOn"
    android:text="Button 1" />
<Button
    android:id="@+id/but2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:background="@drawable/background_selector"
    android:onClick="buttonOn"
    android:text="Button 2" />
<Button
    android:id="@+id/but3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:background="@drawable/background_selector"
    android:onClick="buttonOn"
    android:text="Button 3" />

Here is my selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/background_blue"
    android:state_pressed="true"/>

<item android:drawable="@drawable/background_blue"
    android:state_focused="true"/>

<item android:drawable="@drawable/background_white"/>
</selector>

Here is background_white

<solid android:color="@android:color/white" />
<padding
    android:bottom="1dp"
    android:left="1dp"
    android:right="1dp"
    android:top="1dp" />
</shape>

Here is background_blue

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_blue_bright" />
<padding
    android:bottom="1dp"
    android:left="1dp"
    android:right="1dp"
    android:top="1dp" />
</shape>

Here is my java code

public class MainActivity extends AppCompatActivity {
    Button but1;
    Button but2;
    Button but3;
    Button but4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but1 = (Button) findViewById(R.id.but1);
        but2 = (Button) findViewById(R.id.but2);
        but3 = (Button) findViewById(R.id.but3);
        but4 = (Button) findViewById(R.id.but4);
    }

    public void buttonOn(View v) {
        Fragment view;
        switch(v.getId()) {
            case R.id.but1:
                Log.i("Button 1", "pressed");
                break;
            case R.id.but2:
                Log.i("Button 2", "pressed");
                break;
            case R.id.but3:
                Log.i("Button 3", "pressed");
                break;
            case R.id.but4:
                Log.i("Button 4", "pressed");
                break;
        }
    }

Add background as a drawable selector to button.Also add android:focusableInTouchMode="true" to make button take touch when pressed.

<Button
    android:id="@+id/but1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:background="@drawable/background_selector"
    android:onClick="buttonOn"
    android:text="Button 1" />

Then add two drawables for button selected and unselected inside background_selector

background_selector.xml

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

    <item android:drawable="@drawable/background_blue"
android:state_pressed="true"/>

    <item android:drawable="@drawable/background_blue"
android:state_focused="true"/>

    <item android:drawable="@drawable/background_white"/>
</selector>

And then add the background_white and background_blue to drawable folder

background_white.xml

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

    <solid android:color="@android:color/white" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>

background_blue.xml

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

    <solid android:color="@android:color/holo_blue_bright" />

   <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>
public void changeButtonColors(ViewGroup layout, int clickedButton_id) {
    for (int i = 0; i < layout.getChildCount(); i++) {
            View v = layout.getChildAt(i);
            if (v instanceof Button) {
                if (v.getId() == clickedButton_id){
                    ((Button)v).setBackgroundColor(Color.RED);
                }else{
                    ((Button)v).setBackgroundColor(Color.BLUE);
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM