groupe de radio dynamique Android
public class ActivityMain extends AppCompatActivity implements View.OnClickListener {
EditText mEtNumOfRadioBtns;
Button mBtnAdd;
String TAG = "TestActivity";
RadioGroup mRgAllButtons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
mEtNumOfRadioBtns = findViewById(R.id.et_no);
mBtnAdd = findViewById(R.id.btn);
mRgAllButtons = findViewById(R.id.radiogroup);
//
mBtnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number = Integer.parseInt(mEtNumOfRadioBtns.getText().toString().trim());
addRadioButtons(number);
}
});
}
public void addRadioButtons(int number) {
mRgAllButtons.setOrientation(LinearLayout.HORIZONTAL);
//
for (int i = 1; i <= number; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
rdbtn.setText("Radio " + rdbtn.getId());
rdbtn.setOnClickListener(this);
mRgAllButtons.addView(rdbtn);
}
}
@Override
public void onClick(View v) {
Log.d(TAG, " Name " + ((RadioButton)v).getText() +" Id is "+v.getId());
}
}
Unusual Unicorn