CheckBox, RadioGroup, RadioButton 이벤트 예제

 

(1) activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 <CheckBox
     android:id="@+id/chk1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:checked="true"
     android:text="@string/font1"
     android:layout_alignParentTop="true"
     />

 <RadioGroup
     android:id="@+id/colors"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_below="@id/chk1"
     android:orientation="vertical"  >

 

 <RadioButton
         android:id="@+id/rgRed"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/red"
         android:checked="true"
         />
     <RadioButton
         android:id="@+id/rgBlue"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/blue"
         />
     <RadioButton
         android:id="@+id/rgGreen"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/green"
         />
 </RadioGroup>

  <TextView android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/mymsg"
        android:textSize="20pt"
        android:textColor="#ff0000"
        />
</RelativeLayout>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2) string.xml ( values폴더 )

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

    <string name="app_name">Test04</string>
    <string name="mymsg">Happy Day!</string>
    <string name="font1">글씨 크게</string>
    <string name="red">빨강</string>
    <string name="blue">파랑</string>
    <string name="green">초록</string>

</resources>

 

 

(3) MainActivity.java

package com.example.test04;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
    CheckBox chk1;
    TextView txt1;
    RadioGroup rg;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //텍스트뷰, 체크박스에 대한 참조값 얻어오기.
        chk1 = (CheckBox)findViewById(R.id.chk1);
        txt1 = (TextView)findViewById(R.id.txt1);
        rg=(RadioGroup)findViewById(R.id.colors);

 

        //체크박스에 이벤트리스너 등록(체크상태가 바뀔때마다 감지)
        chk1.setOnCheckedChangeListener( new OnCheckedChangeListener() {
            //체크박스를 클랙해서 체크상태가 바뀔때마다 자동 호출
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //이벤트가 발생된 컴포넌트 id가 chk1이라면 

                if(buttonView.getId()==R.id.chk1){
                    if(isChecked){//체크된 상태라면
                        txt1.setTextSize(40);//텍스트뷰 사이즈 변경
                    }else{
                        txt1.setTextSize(20);
                    }
                }
            }
        });       

 

 

        rg.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {           
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //체크가 된 radio button 아이디
                switch(checkedId){
                //textView의 색상 바꾸기.
                case R.id.rgRed:
                    txt1.setTextColor(Color.RED);break;
                case R.id.rgBlue:
                    txt1.setTextColor(Color.BLUE);break;
                case R.id.rgGreen:
                    txt1.setTextColor(Color.GREEN);break;
                }               
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }   
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Mobile > Android' 카테고리의 다른 글

#07 ( 다음,이전 버튼으로 사진이동하기 )  (0) 2013.02.02
#06 (이미지 뷰)  (0) 2013.02.02
#04( 입력한 값 보여주기 예제 )  (0) 2013.01.28
#03( 합구하기 )  (0) 2013.01.28
#02 ( 시작하기 )  (0) 2013.01.28

+ Recent posts