2015-09-27 4 views
1

Я пытаюсь создать radioButtons. Дело в том, что количество radioButtons, которые я хочу создать, является переменной. Как следствие, я должен реализовать их создание в Java-коде моего проекта Android (не в xml). Моя проблема: я не уверен, как я должен добавить эти radioButtons в пользовательский интерфейс.Android: Создание radioButtons во время выполнения в Java-коде

for(int i=0; i< quizQuestion.getAnswers().size(); i++){ 
     radioButtons[i] = new RadioButton(this); 
     rb[i].setText(quizQuestion.getAnswers().get(i)); 

     //How do I add this to the UI? 
} 

Спасибо.

+0

У вас есть код, который вы можете показать нам? что ты уже испробовал? – emerssso

+0

Я просто создал radioButtons действительно –

+0

Вы хотите найти решение вашей проблемы? Пожалуйста, поделитесь этим кодом, чтобы другие могли извлечь из этого выгоду. –

ответ

0

Простой пример. Надеюсь, это поможет.

MainActivity.java

public class MainActivity extends AppCompatActivity { 

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

     //// 

     LinearLayout radioPanel= (LinearLayout) findViewById(R.id.radioPanel); 

     for (int i = 0; i < 3; i++) { 
      RadioButton radioButton=new RadioButton(this); 

      radioButton.setSelected(false); 

      radioButton.setLeft(16); 
      radioButton.setRight(16); 

      radioButton.setText("RadioButton "+ i); 

      //Here you add these radioButton to the UI 
      radioPanel.addView(radioButton); 
     } 


     //// 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
<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" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:id="@+id/contentPanel" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 







<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 



<!--For RadioButton Content Area. (LinearLayout)--> 

<!--You can use other layouts. This is just one example.--> 

<LinearLayout 
    android:id="@+id/radioPanel" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="200dp" 
    android:layout_below="@+id/button" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

</LinearLayout> 




<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/radioPanel" 
    android:text="New Button 2" 

    /> 

Смежные вопросы