728x90
선택할 수 있는 항목 들을 제공하고 체크를 통해 선택할수 있도록 하는 View
다수의 CheckBox를 동시에 선택할 수 있다.
주요 속성
text : CheckBox에 표시되는 문자열을 설정한다.
checked : 체크 상태를 설정한다.
주요 메서드
toggle : 현재 체크 상태를 반전시킨다.
주요 이벤트
checkedChange : 체크 상태가 변경되는 사건
package com.example.checkbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CompoundButton
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener{
textView.text=""
if (checkBox.isChecked == true) {
textView.append("첫 번쨰 체크박스가 체크되어 있습니다.\n")
} else {
textView.append("첫 번쨰 체크박스가 해제되어 있습니다.\n")
}
if (checkBox2.isChecked == true) {
textView.append("두 번쨰 체크박스가 체크되어 있습니다.\n")
} else {
textView.append("두 번쨰 체크박스가 해제되어 있습니다.\n")
}
if (checkBox3.isChecked == true) {
textView.append("세 번쨰 체크박스가 체크되어 있습니다.\n")
} else {
textView.append("세 번쨰 체크박스가 해제되어 있습니다.\n")
}
}
button2.setOnClickListener {
checkBox.isChecked=true
checkBox2.isChecked=true
checkBox3.isChecked=true
}
button3.setOnClickListener {
checkBox.isChecked=false
checkBox2.isChecked=false
checkBox3.isChecked=false
}
button4.setOnClickListener {
checkBox.toggle()
checkBox2.toggle()
checkBox3.toggle()
}
checkBox.setOnCheckedChangeListener(listener1)
checkBox2.setOnCheckedChangeListener(listener1)
checkBox3.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked == true) {
textView.text="세 번쨰 체크박스가 체크되었습니다."
}else{
textView.text="세 번쨰 체크박스가 해제되었습니다."
}
}
}
val listener1 = object: CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
when (buttonView?.id) {
R.id.checkBox->{
if (isChecked == true) {
textView.text="첫 번쨰 체크박스가 체크되었습니다."
}else{
textView.text="첫 번쨰 체크박스가 해제되었습니다."
}
}
R.id.checkBox2->{
if (isChecked == true) {
textView.text="두 번쨰 체크박스가 체크되었습니다."
}else{
textView.text="두 번쨰 체크박스가 해제되었습니다."
}
}
}
}
}
}

728x90
'안드로이드 > Widget' 카테고리의 다른 글
안드로이드 Switch (0) | 2022.05.30 |
---|---|
안드로이드 RadioButton (0) | 2022.05.30 |
안드로이드 ToggleButton (0) | 2022.05.30 |
안드로이드 ImageView (0) | 2022.05.30 |
안드로이드 TextInputLayout (0) | 2022.05.30 |