简体   繁体   中英

Android Radio Button color change after click action

RadioGroup rg = new RadioGroup(context);
for (int i = 0; i < formElement.getRadioOptions().size(); i++) {
                RadioButton radiobutton1 = new RadioButton(context);
                radiobutton1.setText(formElement.getRadioOptions().get(i).getValue());
                radiobutton1.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.meetingNoteCL));
                radiobutton1.setHighlightColor(context.getResources().getColor(R.color.text_color));
                rg.addView(radiobutton1);
            }
            textInputLayout.setTypeface(FontUtils.getFontTypeRegular(context));
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // checkedId is the RadioButton selected
                   
                    int radioButtonID = group.getCheckedRadioButtonId();
                    View radioButton = group.findViewById(radioButtonID);
                    int idx = group.indexOfChild(radioButton);
                    formElement.setKey(formElement.getRadioOptions().get(idx).getKey());
                    formElement.setValue(formElement.getRadioOptions().get(idx).getValue());

                }
            });

above code for radio button., but after choose I need to change the particular radio button color alone to blue. can u help on this

The onCheckedChanged callback has a checkedId which you can use to get that particular button

You just need to assign your buttons unique ids before adding them to the group. Here is your your code modified to achieve that.

RadioGroup rg = new RadioGroup(context);
for (int i = 0; i < formElement.getRadioOptions().size(); i++) {
                RadioButton radiobutton1 = new RadioButton(context);

//set a unique id

radiobutton1.setId(View.generateViewId());
              radiobutton1.setText(formElement.getRadioOptions().get(i).getValue());
                radiobutton1.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.meetingNoteCL));
                radiobutton1.setHighlightColor(context.getResources().getColor(R.color.text_color));
                rg.addView(radiobutton1);
            }
            textInputLayout.setTypeface(FontUtils.getFontTypeRegular(context));
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // checkedId is the RadioButton selected
                   
                    int radioButtonID = group.getCheckedRadioButtonId();
                    RadioButton radioButton = (RadioButton)group.findViewById(radioButtonID);

//if its checked then change it's tintlist

if(radioButton.isChecked()){
radioButton.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(context,R.color.yourcolor)));
}
                    int idx = group.indexOfChild(radioButton);
                    formElement.setKey(formElement.getRadioOptions().get(idx).getKey());
                    formElement.setValue(formElement.getRadioOptions().get(idx).getValue());

                }
            });

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