簡體   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;
 }

所以我目前正在像這樣實現我的代碼,可以很好地完成工作,但是我有6個按鈕,所以我必須這樣做6次。 我已經閱讀了有關按鈕數組的內容,並嘗試實現它,但是無法使其工作。 對我來說不清楚的是,我怎么知道我單擊了哪個按鈕並將其更改為另一種顏色,而另一個未單擊的按鈕又恢復為其默認顏色。

編輯:
抱歉,如果我不清楚,則此按鈕用於多項選擇。 這些按鈕在創建時已設置為默認值。 如果我依次單擊另一個按鈕,則不使用switch語句會使兩個按鈕具有相同的顏色,它們將具有相同的顏色。 這更多是顯示問題。

無需switch語句。 只需將所有按鈕設置為默認顏色,然后將所選按鈕設置為所選顏色。

考慮將測試移到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));
      ...

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

更新:

您還可以遍歷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