반응형

Ex04CompoundButton

안드로이드 네이티브 앱 개발 수업 예제#4

 

주요코드

ChecxBox, ToggleButton, Switch, RadioButton같은 CompoundButton(복합버튼) 알아보기

  • res폴더>>layout폴더안에 있는 activity_main.xml문서를 수정하여 화면제작
  • CheckBox 3개와 Button 1개, TextView 1개를 만들어 버튼이 클릭되었을 때 선택(Selected)되어 있는 CheckBox들의 텍스트를 얻어와 TextView에 표시하기
  • CheckBox 버튼의 선택(Selected)상태가 변경될 때마다 선택된 체크박스의 텍스트를 표시하기 - 선택상태 변경 리스너 객체 OnCheckedChangeListener 소개
  • ToggleButton의 선택변화리스너 OnCheckedChangeListener를 통해 On/OFF상태 반응하기
  • Switch 위젯의 사용법은 ToggleButton과 같음. 보여지는 UI모양만 다름. 요즘은 switch 뷰가 더 많이 선호됨

 

실행모습 

1. 시작 화면     2. 체크박스의 선택 후 버튼클릭한 화면     3. Toggle or Switch버튼 선택변경 화면

 

 

실행모습 GIF

 

 

소스코드

# activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <CheckBox
        android:id="@+id/cb01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple"
        android:checked="true"/>
    <CheckBox
        android:id="@+id/cb02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Banana"/>
    <CheckBox
        android:id="@+id/cb03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Orange"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="확인"
        android:onClick="clickBtn"/>

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="꺼짐"
        android:textOn="켜짐"/>

    <Switch
        android:id="@+id/sw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sound"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="result"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#000000"/>


</LinearLayout>

 

 

# MainActivity.java
package com.kitesoft.ex04compoundbutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    CheckBox cb01, cb02, cb03;
    TextView tv;

    ToggleButton toggleButton;
    Switch sw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cb01= findViewById(R.id.cb01);
        cb02= findViewById(R.id.cb02);
        cb03= findViewById(R.id.cb03);

        tv= findViewById(R.id.tv);

        //체크박스의 체크상태가 변경되는것을 듣는 리스너객체 생성 및 추가
        CompoundButton.OnCheckedChangeListener changeListener= new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //체크상태가 변경될때 마다 실행되는 메소드
                String s="";

                if( cb01.isChecked() ) s += cb01.getText().toString();
                if( cb02.isChecked() ) s += cb02.getText().toString();
                if( cb03.isChecked() ) s += cb03.getText().toString();

                tv.setText( s );

            }
        };

        cb01.setOnCheckedChangeListener(changeListener);
        cb02.setOnCheckedChangeListener(changeListener);
        cb03.setOnCheckedChangeListener(changeListener);


        toggleButton= findViewById(R.id.toggle);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                tv.setText(  isChecked+"" );

            }
        });

        sw= findViewById(R.id.sw);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                tv.setText( isChecked+"" );
            }
        });


    }

    //onClick속성이 부여된 View가 클릭되면 자동으로
    //실행되는 콜백메소드
    public void clickBtn(View v){

        //선택되어 있는 CheckBox의 글씨 얻어오기
        String s="";

        if( cb01.isChecked() ) s += cb01.getText().toString();
        if( cb02.isChecked() ) s += cb02.getText().toString();
        if( cb03.isChecked() ) s += cb03.getText().toString();

        tv.setText( s );

    }

}

 

 


 

 

 

 

kitesoft2058/Ex04CompoundButton

안드로이드 네이티브 앱 개발 수업 예제#4. Contribute to kitesoft2058/Ex04CompoundButton development by creating an account on GitHub.

github.com

 

반응형

+ Recent posts