<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cb1"
android:checked="true"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cb2" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cb3" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 확인" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 하기" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 해제" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크 반전" />
</LinearLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1.setOnClickListener { view ->
tv1.text = ""
if (cb1.isChecked)
tv1.append("체크 박스 1 체크")
if (cb2.isChecked)
tv1.append("체크 박스 2 체크")
if (cb3.isChecked)
tv1.append("체크 박스 3 체크")
}
btn2.setOnClickListener { view ->
cb1.isChecked = true
cb2.isChecked = true
cb3.isChecked = true
}
btn3.setOnClickListener { view ->
cb1.isChecked = false
cb2.isChecked = false
cb3.isChecked = false
}
btn4.setOnClickListener { view ->
cb1.toggle()
cb2.toggle()
cb3.toggle()
}
var listener = CheckBoxListener()
cb1.setOnCheckedChangeListener(listener);
cb2.setOnCheckedChangeListener{compoundButton, b ->
if(b)
tv1.text = "Event : CheckBox2 Checked"
else
tv1.text = "Event CheckBox2 unChecked"
}
cb3.setOnCheckedChangeListener{compoundButton, b ->
if(b)
tv1.text = "Event : CheckBox3 Checked"
else
tv1.text = "Event CheckBox3 unChecked"
}
}
inner class CheckBoxListener : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
if(p1)
tv1.text = "Event : CheckBox1 Checked"
else
tv1.text = "Event CheckBox1 unChecked"
}
}
}
체크 박스는 기본적으로 True False로 작동한다. true를 넣으면 체크되고 false를 넣으면 체크가 해제되고.. 아주 기본적인 View이다.
자바랑 조금 다른점을 찾자면 전부 람다식에서 시작되는 것 같다. 그리고 View를 다운캐스팅하여 boolean 값을 찾을 필요도 없다. Simple! 상황에 맞는 방식을 도입하면 된다.
궁금하신 점은 덧글로 적어주세요.
'안드로이드' 카테고리의 다른 글
[Android Kotlin] 안드로이드 코틀린 Seek Bar (0) | 2020.03.21 |
---|---|
[Android Kotlin] 안드로이드 코틀린 Progress Bar (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Radio Button (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Button (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 TextView (0) | 2020.03.21 |