简体   繁体   中英

Changing color of a button in an array of buttons and return the other buttons to default

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.btnA:
            runOnUiThread(new Thread(new Runnable() {
                @Override
                public void run() {
                    btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
                    btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                }
            }));
            break;
        case R.id.btnB:
            runOnUiThread(new Thread(new Runnable() {
                @Override
                public void run() {
                    btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
                    btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));                
                }
            }));
            break;
 }

So I am currently implementing my code like this it does the job fine, but I have 6 buttons so I have to do this 6 times. I've read about array of buttons and tried to implement it but could not make it work. What isn't clear to me is how do I know which button I clicked and changed it to another color while the other button that is not clicked goes back to their default color.

EDIT:
Sorry if I wasn't clear, this buttons are used for multiple choices. The buttons are already set to default on creation. Not using the switch statement would make the two buttons the same color if I click on another button after the other, they would be the same color. It's more of a display problem..

No need for a switch statement. Just set all buttons to the default color, then set the selected button to the selected color.

Consider moving the test into the setBackgroundColor call and keeping it all in a single new Runnable

@Override
public void onClick(View v) {
  runOnUiThread(new Thread(new Runnable() {
    @Override
    public void run() {
      btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), (v.getId() == R.id.btnA ? R.color.button_pressed : R.color.colorPrimary));
      btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), (v.getId() == R.id.btnB ? R.color.button_pressed : R.color.colorPrimary));
      ...

I guess what FredK meant was something like that:

    @Override
    public void onClick(View v) {
        runOnUiThread(new Thread(new Runnable() {
            @Override
            public void run() {
                // Reset all buttons
                btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                .
                .
                .
                btnZ.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));

                // Set only the clicked button
                v.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
            }
        }));
    }

UPDATE:

You can also iterate over the ViewGroup so you won't need to write down each Button manually.

    ViewGroup viewGroup = (ViewGroup) v.getParent();
    for(int i=0;i<viewGroup.getChildCount();i++){
        Object child = viewGroup.getChildAt(i);
        if(child instanceof Button){
            ((Button) child).setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        }
    }

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