Как создать радио кнопку, не зная точно счет? - PullRequest
0 голосов
/ 09 августа 2011

Как создать радиокнопку в radioGroup в файле макет без точного подсчета

У кого-нибудь есть идеи?

Ответы [ 2 ]

3 голосов
/ 09 августа 2011

Прошу вас четко сказать, но, как я понял, вы хотите много радиокнопок,

Take a loop and create dynamic RadioButton add these button to Radio Group  
2 голосов
/ 09 августа 2011

Может быть, это поможет;

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>

Этот код добавляет переключатели от нажатия кнопки, выможете сделать то же самое из цикла, если вы хотите добавить несколько кнопок одновременно.Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...