상세 컨텐츠

본문 제목

[android]라디오 그룹 이벤트

Android 개발

by mobile 2013. 3. 10. 04:18

본문

반응형

사용자가 라디오 버튼을 클릭하면 단순하게 화면상의 변화뿐만 아니라 프로그램 내부에서도 라디오버튼의 변화와 라디오 그룹 내 변화에 대해 리스너를 통해 알 수 있다.


RadioGroup 클래스는 android.widget.LinearLayout 클래스를 상속받는다.

RadioGroup.OnCheckedChangedListener 인터페이스는 라디오 버튼으로 이루어진 라디오 그룹내 라디오 버튼의 클릭 여부에 따른 변화를 파악하는 리스너이다.


onCheckedChanged(RadioGroup group, int checkedId) 콜백 메서드는 라디오 그룹내 라디오 버튼의 변화가 생기면 호출되는 메서드 이다. 사용되는 매개변수는 다음과 같다.

- group : 변화가 발생한 라디오 그룹

- checkedId : 새롭게 체크된 라디오 버튼의 아이디


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <RadioGroup android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical"

            android:checkedButton="@+id/lunch"

            android:id="@+id/menu">

            <RadioButton android:text="Breakfast"

                android:id="@+id/breakfast"/>

            <RadioButton android:text="Lunch"

                android:id="@id/lunch"/>

            <RadioButton android:text="Dinner"

                android:id="@+id/dinner"/>

            <RadioButton android:text="All of them"

                android:id="@+id/all"/>

            <TextView android:text="당신의 선택은 : "

                android:id="@+id/choice"/>

        </RadioGroup>

        <Button android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Clear"

           android:id="@+id/clear"/>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>

<resources>

<item type="id" name="snack"></item>    

</resources>


package com.example.test_sample_02;


import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;


public class MainActivity extends Activity implements

RadioGroup.OnCheckedChangeListener, View.OnClickListener {


private TextView mChoice;

private RadioGroup mRadioGroup;

private String selection;

private RadioButton selectButton;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.radio_group_ex);

mRadioGroup = (RadioGroup) findViewById(R.id.menu);


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);


selection = getString(R.string.radio_group_selection);

mRadioGroup.setOnCheckedChangeListener(this);


selectButton = (RadioButton) findViewById(mRadioGroup

.getCheckedRadioButtonId());

mChoice = (TextView) findViewById(R.id.choice);

// mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());

mChoice.setText(selection + selectButton.getText());


Button clearButton = (Button) findViewById(R.id.clear);

// Register the onClickListener

clearButton.setOnClickListener(this);


}


@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

String none = getString(R.string.radio_group_none);

selectButton = (RadioButton) findViewById(checkedId);

//mChoice.setText(selection + (checkedId == View.NO_ID ? none : checkedId));

mChoice.setText(selection

+ (checkedId == View.NO_ID ? none : selectButton.getText()));

}


@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

// clearCheck() 메서드는 선택한 라디오 버튼 취소하는 기능을 한다.

mRadioGroup.clearCheck();

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

사용자가 라디오 버튼을 클릭하게 되면 라디오그룹내 이벤트가 발생한다. 이벤트는 onCheckedChanged() 콜백 메서드의 매개변수를 통해 사용자가 선택한 라디오 그룹과 라디오 버튼의 아이디로 자바 프로그램에 전달 된다. View.NO_ID는 -1를 나타내는 상수이다.


뷰들이 생성될 때 우리의 뷰의 아이디를 주지 않더라도 안드로이드는 내부적으로 뷰를 구분하기 위해 독자적으로 뷰의 아이디를 부여한다.

반응형

'Android 개발' 카테고리의 다른 글

[Android]RatingBar 이벤트  (0) 2013.03.31
[Android] SeekBar event  (0) 2013.03.24
[Android] 뷰 이벤트  (0) 2013.03.10
[android]뷰의 화면 출력 과정  (0) 2013.03.09
[Android]자바 프로그램으로 화면 구성  (1) 2013.02.26

관련글 더보기

댓글 영역