IT/Android Studio

Android Studio s07CheckBoxDemo

Millennials 2020. 7. 1. 10:49

1)Activity_Main.xml 

 

체크박스 설정 및 id 추가

2)MainActivity.java

package kr.android.s07checkboxdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    CheckBox cb;

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

        cb=(CheckBox)findViewById(R.id.checkBox);
        //이벤트 연결
        cb.setOnCheckedChangeListener(this);
    }

    //이벤트 핸들러
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            cb.setText("체크 상태");
        }else{
            cb.setText("체크되지 않은 상태");
        }
    }

반응형