Views_RadioGroup

old/API_Demo 2010. 5. 6. 14:59

[RadioGroup1.java]

public class RadioGroup1 extends Activity implements RadioGroup.OnCheckedChangeListener,
        View.OnClickListener {

    private TextView mChoice;
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.radio_group_1);
        mRadioGroup = (RadioGroup) findViewById(R.id.menu);

        // test adding a radio button programmatically
        RadioButton newRadioButton = new RadioButton(this);
        newRadioButton.setText(R.string.radio_group_snack);
        newRadioButton.setId(R.id.snack);
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        mRadioGroup.addView(newRadioButton, 0, layoutParams);

        // test listening to checked change events
        String selection = getString(R.string.radio_group_selection);
        mRadioGroup.setOnCheckedChangeListener(this);
        mChoice = (TextView) findViewById(R.id.choice);
        mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());

        // test clearing the selection
        Button clearButton = (Button) findViewById(R.id.clear);
        clearButton.setOnClickListener(this);
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String selection = getString(R.string.radio_group_selection);
        String none = getString(R.string.radio_group_none);
        mChoice.setText(selection +
                (checkedId == View.NO_ID ? none : checkedId));
    }

    public void onClick(View v) {
        mRadioGroup.clearCheck();
    }
}



[radio_group_1.xml]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:checkedButton="@+id/lunch"
        android:id="@+id/menu">
        <RadioButton
            android:text="@string/radio_group_1_breakfast"
            android:id="@+id/breakfast"
            />
        <RadioButton
            android:text="@string/radio_group_1_lunch"
            android:id="@id/lunch" />
        <RadioButton
            android:text="@string/radio_group_1_dinner"
            android:id="@+id/dinner" />
        <RadioButton
            android:text="@string/radio_group_1_all"
            android:id="@+id/all" />
        <TextView
            android:text="@string/radio_group_1_selection"
            android:id="@+id/choice" />
    </RadioGroup>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_group_1_clear"
        android:id="@+id/clear" />
</LinearLayout>


[/res/values/strings.xml]

    <string name="radio_group_1_breakfast">Breakfast</string>
    <string name="radio_group_1_lunch">Lunch</string>
    <string name="radio_group_1_dinner">Dinner</string>
    <string name="radio_group_1_all">All of them</string>
    <string name="radio_group_1_selection">You have selected: (none)</string>
    <string name="radio_group_1_clear">Clear</string>

    <string name="radio_group_snack">Snack</string>
    <string name="radio_group_selection">"You have selected: "</string>
    <string name="radio_group_none">(none)</string>

'old > API_Demo' 카테고리의 다른 글

Views_ScrollBars_Basic  (0) 2010.05.06
Views_RatingBar  (0) 2010.05.06
Views_ProgressBar_Dialogs  (0) 2010.05.06
Views_ProgressBar_Dialogs  (0) 2010.05.06
Views_ProgressBar_Smooth  (0) 2010.05.06
Posted by jazzlife
,