0
如何更改處於選定狀態的RadioButton的背景圖像。我已經使用checkBoxes的代碼,它的工作原理,但對於RadioButtons不起作用。這裏是我的代碼片段:Android:以編程方式更改RadioButton的風格
RadioGroup radioGroup = new RadioGroup(context);
for (int i = 0; i < lst.size(); i++){
// Radiobuttons
RadioButton radioButton = new RadioButton(context);
radioButton.setId(i);
radioButton.setButtonDrawable(myDrawable);
radioButton.setText(myTxt);
radioGroup.addView(radioButton, i, layoutParams);
}
this.addView(radioGroup);
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
StateListDrawable myStates = new StateListDrawable();
int stateSelected = android.R.attr.state_selected;
myStates.addState(new int[]{ -stateSelected }, myDrawable2);
RadioButton r = (RadioButton) group.getChildAt(checkedId);
r.setButtonDrawable(myStates);
}