Android settings button changes color after clicking (navigation bar changes color)

Layout sets the id of each button

<RadioGroup
            android:id="@+id/rg_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_home"
                style="@style/nav_style"
                android:checked="true"
                android:drawableTop="@drawable/nav_draw_home"
                android:text="@string/nav_home"/>

            <RadioButton
                android:id="@+id/rb_query"
                style="@style/nav_style"
                android:drawableTop="@drawable/nav_draw_query"
                android:text="@string/nav_query"/>

            <RadioButton
                android:id="@+id/rb_recite"
                style="@style/nav_style"
                android:drawableTop="@drawable/nav_draw_recite"
                android:text="@string/nav_recite"/>

            <RadioButton
                android:id="@+id/rb_exam"
                style="@style/nav_style_exam"
                android:drawableTop="@drawable/nav_draw_exam"
                android:text="@string/nav_exam"/>

            <RadioButton
                android:id="@+id/rb_person"
                style="@style/nav_style_person"
                android:drawableTop="@drawable/nav_draw_person"
                android:text="@string/nav_person"/>
        </RadioGroup>

MainActivity binds OnClickListener and overrides the onClick method

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_home:
                setChangeColor(btn_home);
                break;
            case R.id.rb_query:
                setChangeColor(btn_query);
                break;
            case R.id.rb_recite:
                setChangeColor(btn_recite);
                break;
            case R.id.rb_exam:
                setChangeColor(btn_exam);
                break;
            case R.id.rb_person:
                setChangeColor(btn_person);
                break;
        }
    }

    private void setChangeColor(RadioButton btn) {
        List<RadioButton> buttonList=new ArrayList<>();
        if (buttonList.size()==0){
            buttonList.add(btn_home);
            buttonList.add(btn_query);
            buttonList.add(btn_recite);
            buttonList.add(btn_exam);
            buttonList.add(btn_person);
        }
        for (int i = 0; i <buttonList.size() ; i++) {
            buttonList.get(i).setTextColor(0xffA5A5A5);
        }
        if(btn==btn_recite){
            btn.setTextColor(0xffEACE8B);
        }
        else{
            btn.setTextColor(0xff5F85A1);
        }
    }

The parameter of setTextColor() method is hexadecimal color code. 0x represents the hexadecimal system, ff represents the alpha value, and the remaining six bits correspond to the hexadecimal system of R, G, and B respectively
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43873198/article/details/108911610