라디오 버튼은 <RadioButton>을 사용한다.


<RadioGroup>을 사용하여 라디오 버튼을 그룹화 할 수 있다.


라디오 그룹의 setOnCheckedChangeListener 이벤트를 사용하여 클릭한 라디오 버튼의 ID를 알 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼1" />
 
        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼2" />
 
 
        <RadioButton
            android:id="@+id/radiobutton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼3" />
    </RadioGroup>
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


'Android' 카테고리의 다른 글

[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12
[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06

+ Recent posts