checkbox
체크박스란 여러 항목을 제공하고 체크를 통해 사용자가 선택할 수 있도록 하는 뷰 입니다.
주요 속성 ㅡㅡㅡㅡㅡ
-text
체크박스에 표시되는 문자열을 설정합니다.
-checked
체크 박스의 체크 상태를 설정합니다.
주요 속성(프로퍼티) ㅡㅡㅡㅡㅡ
-ischecked
체크박스의 체크상태를 확인 (true /false)합니다.
1
2
| checkBox.isChecked =false
checkBox.isChecked =true
|
주요 메소드 ㅡㅡㅡㅡㅡ
-toggle()
현재 체크박스의 체크 상태를 반전 시킵니다.
주요 리스너 ㅡㅡㅡㅡㅡ
-OnCheckedChangeListener{}
체크 상태가 변경되었을 때 반응하는 리스너입니다.
1
2
3
4
5
6
7
8
| //buttonView =체크가 변경된 뷰 아이디
//isChecked =그 뷰의 체크 상
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked ==true)
textView.text ="체크박스 1이 체크 되었습니다."
else
textView.text ="체크박스 1이 체크 해제 되었습니다."
}
|
먼저 간단한 예제를 살펴보겠습니다.
완성된 이 화면은 간단한 구조들로 이루어져 있습니다.
최상단에 텍스트뷰가 있어 하단에 있는 2개의 체크박스의 상태 변경에 따라 값을 나타내 주고,
하단의 버튼은 체크박스의 결과값을 정리할 수 있습니다.
먼저 코드를 보겠습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| <?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/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="텍뷰"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox1"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox2"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
|
간단한 예제 어플의 XML로 만든 레이아웃의 구조입니다.
그렇다면 이제 위젯에 생명을 부여해 줄 코틀린 코드를 살펴보겠습니다.
ㄴㅇ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener { v: View? ->
textView.text =" v.v"
if (checkBox.isChecked ==true)
textView.append("\n1번 체크박스가 설정 되었습니다. ")
}
//buttonView =체크가 변경된 뷰 아이디
//isChecked =그 뷰의 체크 상태
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked ==true)
textView.text ="체크박스 1이 체크 되었습니다."
else
textView.text ="체크박스 1이 체크 해제 되었습니다."
}
}
}
|
2개의 동일한 체크박스 중 한가지만 가지고 살펴보겠습니다.
먼저 15번째 줄입니다.
checkBox.setOnCheckedChangeListener {~~ , 즉 checkBox의 아이디를 가지고 있는 체크박스의 리스너인 setOnCheckedChangeListener를 람다식으로 열었습니다.
그리고 buttonView, isCheck -> 부분에서는 buttonView는 체크 상태가 변경된 뷰의 아이디값을 불러옵니다.
그리고 isChecked 부분은 그 체크 상태가 변경된 뷰의 체크 상태의 값을 담게 됩니다.
즉 if문을 전개해 isCheck를 비교해보면 해당 뷰의 체크값을 가지고 다른 작업을 수행할 수 있게 됩니다.
주요 속성(프로퍼티)
-ischecked
체크박스의 체크상태를 확인 (true /false)합니다.
주요 메소드
-toggle
현재 체크박스의 체크 상태를 반전 시킵니다.
-OnCheckedChangeListener
체크 상태가 변경되었을 때 반응하는 리스너입니다.
먼저 간단한 예제를 살펴보겠습니다.
완성된 이 화면은 간단한 구조들로 이루어져 있습니다.
최상단에 텍스트뷰가 있어 하단에 있는 2개의 체크박스의 상태 변경에 따라 값을 나타내 주고,
하단의 버튼은 체크박스의 결과값을 정리할 수 있습니다.
먼저 코드를 보겠습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| <?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/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="텍뷰"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox1"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox2"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
|
간단한 예제 어플의 XML로 만든 레이아웃의 구조입니다.
그렇다면 이제 위젯에 생명을 부여해 줄 코틀린 코드를 살펴보겠습니다.
ㄴㅇ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener { v: View? ->
textView.text =" v.v"
if (checkBox.isChecked ==true)
textView.append("\n1번 체크박스가 설정 되었습니다. ")
}
//buttonView =체크가 변경된 뷰 아이디
//isChecked =그 뷰의 체크 상태
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked ==true)
textView.text ="체크박스 1이 체크 되었습니다."
else
textView.text ="체크박스 1이 체크 해제 되었습니다."
}
}
}
|
2개의 동일한 체크박스 중 한가지만 가지고 살펴보겠습니다.
먼저 15번째 줄입니다.
checkBox.setOnCheckedChangeListener {~~ , 즉 checkBox의 아이디를 가지고 있는 체크박스의 리스너인 setOnCheckedChangeListener를 람다식으로 열었습니다.
그리고 buttonView, isCheck -> 부분에서는 buttonView는 체크 상태가 변경된 뷰의 아이디값을 불러옵니다.
그리고 isChecked 부분은 그 체크 상태가 변경된 뷰의 체크 상태의 값을 담게 됩니다.
즉 if문을 전개해 isCheck를 비교해보면 해당 뷰의 체크값을 가지고 다른 작업을 수행할 수 있게 됩니다.
댓글
댓글 쓰기