2011-08-09 50 views

回答

2

可能這是任何幫助;

public class RadDemoActivity extends Activity { 

    private RadioGroup radGroup; 
    private Button addButton; 
    private RadioButton radButton; 

    private static int no; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     no=0; 

     doInflateItems(); 

     setAddButton(); 
    } 


    private void doInflateItems() { 

     radGroup = (RadioGroup) findViewById(R.id.radGroup); 
     addButton = (Button) findViewById(R.id.addRad); 
    } 


    private void setAddButton() { 

     addButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       radButton = new RadioButton(getApplicationContext()); 
       radButton.setText("Option " + no++); 

       radGroup.addView(radButton); 
      } 
     }); 
    } 
} 

XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button android:id="@+id/addRad" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Add" /> 


    <ScrollView android:id="@+id/vScroll" android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <RadioGroup android:id="@+id/radGroup" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

    </ScrollView> 
</LinearLayout> 

此代碼是從一個按鈕,點擊添加單選按鈕,你可以從一個循環,如果你想一次添加多個按鈕的功能是相同的。希望這可以幫助。

+0

哇!非常感謝你 – android

+0

@android:我的榮幸。 :) – Mudassir

3

我請你說清楚的事情,但我得到了,你想多數量的單選按鈕,

Take a loop and create dynamic RadioButton add these button to Radio Group 
+0

感謝您的幫助^^ – android

相關問題