简体   繁体   English

更改按钮数组中按钮的颜色并将其他按钮恢复为默认值

[英]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. 所以我目前正在像这样实现我的代码,可以很好地完成工作,但是我有6个按钮,所以我必须这样做6次。 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. 如果我依次单击另一个按钮,则不使用switch语句会使两个按钮具有相同的颜色,它们将具有相同的颜色。 It's more of a display problem.. 这更多是显示问题。

No need for a switch statement. 无需switch语句。 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 考虑将测试移到setBackgroundColor调用中,并将其全部保存在一个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: 我想FredK的意思是这样的:

    @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,因此您无需手动写下每个Button。

    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));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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