xmlレイアウトには、RadioGroup、Button1、Button2があります。ユーザーがbutton1をクリックすると、RadioGroupにいくつかのラジオボタンがプログラムで作成されます(ラジオボタンの総数は異なる場合があります(pocet =作成するラジオボタンの数)。
final RadioButton[] rb = new RadioButton[pocet];
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
radiobuttonCount++;
for(int i=0; i<pocet; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Radio Button " + radiobuttonCount);
rb[i].setId(radiobuttonCount+i);
rb[i].setBackgroundResource(R.drawable.button_green);
rg.addView(rb[i]);
}
私がやろうとしていることはこれです:ユーザーがRadioGroupからxyアイテムを選択すると、選択した値をtextviewに渡し、すべてのradioButtonを削除します。
私が使用する目的を削除するために:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
}
問題は、これがうまく機能することもありますが、最初のラジオボタンが削除されないままになることもあります。
PS後で、ラジオグループにさまざまなアイテムとさまざまなラジオボタンの量を供給するbutton2を追加したいと思います。そのため、ユーザーが選択した後、すべてのラジオボタンを削除する必要があります。