2013-10-15 62 views
14

我試過這個代碼..當模擬器啓動時,它將在單行中顯示三個單選按鈕。但我需要一個按鈕事件。即;如果我點擊按鈕,它應該詢問單選按鈕的數量。那麼如果我給出計數,它必須根據給定的計數顯示單選按鈕。例如,如果我將計數設置爲3,則它必須在單行中顯示三個單選按鈕。 非常感謝您的幫助。 在此先感謝。如何按給定數量的計數動態添加單選按鈕?

public class MyActivity extends Activity { 
    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     for(int row=0; row < 1; row++) 
     { 
      LinearLayout ll = new LinearLayout(this); 
      ll.setOrientation(LinearLayout.HORIZONTAL); 
      for(int i = 1; i < 4; i++) { 
       RadioButton rdbtn = new RadioButton(this); 
       rdbtn.setId((row * 2) + i); 
       rdbtn.setText("Radio " + rdbtn.getId()); 
       ll.addView(rdbtn); 
      } 
      ((ViewGroup)findViewById(R.id.radiogroup)).addView(ll); 
     } 
    } 
    } 

這是XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".MainActivity" > 

    <RadioGroup 
      android:id="@+id/radiogroup" 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true"/> 

    </RelativeLayout> 

回答

28

請找到下面的代碼,我已經在XML中創建一個 '的EditText' 和一個 '按鈕'佈局。在'EditText'中輸入一個數字並點擊按鈕,同樣的數字。的單選按鈕將被添加到佈局中。

這是您的ActivityMain

public class ActivityMain extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     final EditText editText=(EditText)findViewById(R.id.et_no); 

     findViewById(R.id.btn).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       int number=Integer.parseInt(editText.getText().toString()); 
       addRadioButtons(number); 
      } 
     }); 

    } 

    public void addRadioButtons(int number) { 

    for (int row = 0; row < 1; row++) { 
     RadioGroup ll = new RadioGroup(this); 
     ll.setOrientation(LinearLayout.HORIZONTAL); 

     for (int i = 1; i <= number; i++) { 
      RadioButton rdbtn = new RadioButton(this); 
      rdbtn.setId((row * 2) + i); 
      rdbtn.setText("Radio " + rdbtn.getId()); 
      ll.addView(rdbtn); 
     } 
     ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll); 
    } 

    } 

} 

,這裏是你的佈局文件

<RadioGroup 
    android:id="@+id/radiogroup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" /> 



<LinearLayout 
    android:layout_marginTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 

    <EditText android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:hint="Enter no." 
     android:inputType="number" 
     android:id="@+id/et_no"/> 


    <Button 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="Add Radio btn" 
     android:id="@+id/btn"/> 
</LinearLayout> 

+0

非常感謝你..它工作正常! –

+1

以防萬一有人在同一個問題上絆倒: 我試過這個解決方案,但是顯示的層次結構(radiogroup-> linearlayout-> radiobutton)我沒有得到檢查的單選按鈕:((RadioGroup)findViewById(R.id .radiogroup))getCheckedRadioButtonId(); 我需要切換線性佈局,以便將radiobutton直接添加到radiogroup中(如linearlayout-> radiogroup-> radiobutton)。 –

+3

這工作正常,但允許在同一時間檢查多個單選按鈕!爲防止出現這種情況,只需將單選按鈕添加到線性佈局,直接添加到單選按鈕的組。 –

18

嘗試類似如下:

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup); 
RadioGroup.LayoutParams rprms; 

for(int i=0;i<3;i++){ 
     RadioButton radioButton = new RadioButton(this); 
     radioButton.setText("new"+i); 
     radioButton.setId("rbtn"+i); 
     rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     rgp.addView(radioButton, rprms); 
} 
+0

它爲我工作.. –

+0

@Riser如何從選定的單選按鈕獲取文本,請幫助我 –

1

廣告達Button和XML EditText,並採取輸入從EDITTEXT可變inputValue和嘗試這個,

public class MyActivity extends Activity { 

    /** 
    * Called when the activity is first created. 
    */ 
    LinearLayout ll=null; 
    ViewGroup vwgroup; 
    Button btnClick; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    vwgroup=((ViewGroup)findViewById(R.id.radiogroup) 
    btnClick=(Button)findViewById(R.id.button1); 


     btnClick.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if(ll!=null) 
        viewgroup.removeView(ll); 
       ll = new LinearLayout(this); 
       for(int i = 1; i < inputValue; i++) { 
        RadioButton rdbtn = new RadioButton(this); 
        rdbtn.setId((row * 2) + i); 
        rdbtn.setText("Radio " + rdbtn.getId()); 
        ll.addView(rdbtn); 
       } 
       vwgroup.addView(ll); 

      } 
     }); 
    } 
    } 
6

這是做到這一點的方式:

RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);   
    int buttons = 5; 
    for (int i = 1; i <= buttons ; i++) { 
     RadioButton rbn = new RadioButton(this); 
     rbn.setId(i + 1000); 
     rbn.setText("RadioButton" + i); 
     rgp.addView(rbn); 
    } 

enter image description here

但是,如果你需要什麼要做到這一點水平,只需添加方向(默認值是垂直)與setOrientation()方法:

RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group); 
    rgp.setOrientation(LinearLayout.HORIZONTAL); 
    int buttons = 5; 
    for (int i = 1; i <= buttons; i++) { 
     RadioButton rbn = new RadioButton(this); 
     rbn.setId(1 + 1000); 
     rbn.setText("RadioButton" + i); 
     rbn.setLayoutParams(params); 
     rgp.addView(rbn); 
    } 

enter image description here


這是完整的代碼:

第一我們的佈局內的所有定義RadioGroup中的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <RadioGroup 
     android:id="@+id/radio_group" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

代碼到在MainActivity:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Defining 5 buttons. 
     int buttons = 5; 
     AppCompatRadioButton[] rb = new AppCompatRadioButton[buttons]; 

     RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group); 
     rgp.setOrientation(LinearLayout.HORIZONTAL); 

     for (int i = 1; i <= buttons; i++) { 
      RadioButton rbn = new RadioButton(this); 
      rbn.setId(i + 1000); 
      rbn.setText("RadioButton" + i); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f); 
      rbn.setLayoutParams(params); 
      rgp.addView(rbn); 
     } 

    } 
} 
+0

感謝您的支持。有一點需要注意的是,ID必須是唯一的,這是我遇到的問題。 –

+1

當然是(1 + 1000)id是爲id定義的連續數字:rbn.setId(1 + 1000); – Jorgesys

+0

我想要水平映射按鈕,但上面的解決方案似乎對我無效。它可以工作,如果我想垂直而不是水平地映射它。任何幫助? –