[Android] RadioGroup, RadioButton

Study/Android Studio 2021. 2. 6. 08:46 Posted by meanoflife
반응형

RadioGroup과 RadioButton

 

라디오버튼은 여러 개의 항목들 중 하나만 선택해야 할 경우 사용합니다.

여러 항목들 중 하나만 선택되어야 하기 때문에 선택될 수 있는 범위를 지정해야 합니다.

이를 위해 RadioGroup를 사용합니다.

같은 RadioGroup에 속한 RadioButton은 자동으로 하나만 선택이 됩니다.

 

화면 구성하기

 

RadioGroup 하나에 3개의 RadioButton 컴포넌트를 구성합니다.

 

[그림] RadioGroup의 RadioButton

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <RadioButton
        android:id="@+id/rdoLangJava"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:text="Java"
        android:checked="true"
        />
    <RadioButton
        android:id="@+id/rdoLangJavaScript"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:text="JavaScript"
        android:checked="false"
        />
    <RadioButton
        android:id="@+id/rdoLangOracle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:text="Oracle"
        />
</RadioGroup>

[소스] activity.xml

 

선택된 Radio-Button정보 가져오기

 

라디오그룹에서 선택된 라디오버튼 정보를 가져와서 처리하는 소스입니다.

RadioGroup rdgGroup = findViewById( R.id.rdgLang );
RadioButton rdoButton = findViewById( rdgGroup.getCheckedRadioButtonId() );

String strPgmId = rdoButton.getText().toString().toUpperCase();

[소스] Activity.class

 

RadioGroup에서 선택된 RadioButton정보를 가져오기 위해

  ① 레이아웃에서 RadioGroup를 찾습니다.

  ② RadioGroup에서 getCheckedRadioButtonId() 메소드로 선택된 RadioButton을 찾습니다.

  ③ 선택된 RadioButton의 ID정보나 Text정보를 가져옵니다.

 

예시는 getText()로 Text정보를 가져왔고, ID정보는 getId()를 사용하면 됩니다.

 

The End.

 

반응형

'Study > Android Studio' 카테고리의 다른 글

[Android] 메뉴 영역, ActionBar 없애기  (0) 2021.02.06
[Android] NestedScrollView  (0) 2021.02.06
[Android] Activity Flags - 작성중  (0) 2021.02.05
[Android] Activity Stack  (0) 2021.02.05
[Android] View의 이벤트와 리스너  (0) 2021.02.04