2011-07-11 7 views
25

Androidアプリ内の文字列の配列に対応する一連のラジオボタンを作成したいとします。ラジオボタンは、配列から表示されるコンテンツを切り替える必要があります。これはどうすればいいですか?ラジオボタンをプログラムで作成する

+0

あなたはこれまで何かしましたか?あなたの進捗状況を表示してください。 –

答えて

58

あなたはRadioGroupにラジオボタンを追加する必要があり、その後、layout

からRadioGroupは私が提出されているもののようないくつかの情報を見逃すことが、あなたのコードは次のようになります。

private void createRadioButton() { 
    final RadioButton[] rb = new RadioButton[5]; 
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup 
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
    for(int i=0; i<5; i++){ 
     rb[i] = new RadioButton(this); 
     rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout 
     rb[i].setText("Test"); 
    } 
    ll.addView(rg);//you add the whole RadioGroup to the layout 
    ll.addView(submit); 
    submit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      for(int i = 0; i < 5; i++) { 
       rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup 
      } 
      ll.removeView(submit); 
      Questions(); 
     } 
    }); 
} 

の別のコードを動的radiobutton

<TableRow> 
    <RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/radiobuttons"> 
    </RadioGroup> 
</TableRow> 

コードを作成:

public void makeRadioButtons(Vector tmpVector, int i, LinearLayout.LayoutParams lp) 
{ 
    RadioButton rb = new RadioButton(this); 
    rb.setText((String) tmpVector.elementAt(i)); 
    //rg is private member of class which refers to the radio group which I find 
    //by id. 
    rg.addView(rb, 0, lp); 

} 
+0

** ll.removeView(submit)の** ll **は何ですか?** – partho

+0

@partho私はそれが 'LinearLayout'インスタンスを表すと思いますか? – stkent

関連する問題