Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- React
- 답글
- Spring
- 스타일보험
- Android
- sub query
- 상속
- parcel
- Spinner
- 게시판
- FIle
- 오버라이드
- 시큐어코딩
- 쿠키
- 안드로이드
- 스프링
- SQL
- webpack
- 왕초보
- 함수
- snowpack
- java#왕초보
- 코틀린
- 미니게임
- degit
- kotlin
- java
- 버전일치
- 숫자
- git
Archives
- Today
- Total
YSHUSH
체크박스 본문
체크박스의 체크 여부를 텍스트뷰에 보여주도록 하자
1. activity_main.xml
체크박스와 텍스트 뷰를 만들어준다
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
app:layout_constraintVertical_bias="0.201"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. build.gradle설정 추가
buildFeatures{
viewBinding true
}
3. MainActivity.kt
첫번째 방법
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.checkBox.setOnCheckedChangeListener(checklistener)
}
// 체크박스가 여러개일때는 이 방법이 효율적임
val checklistener by lazy {
CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
val checkBox = findViewById<CheckBox>(R.id.checkBox)
val textView = findViewById<TextView>(R.id.textView)
if (isChecked){
when(buttonView.id){
R.id.checkBox ->{
textView.text = "첵크됨"
}
}
}else{
when(buttonView.id){
R.id.checkBox ->{
textView.text = "첵크 해제됨"
}
}
}
}
}
}
두번째 방법
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val checkBox = findViewById<CheckBox>(R.id.checkBox)
val textView = findViewById<TextView>(R.id.textView)
checkBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
textView.text = "첵크됨"
} else {
textView.text = "첵크 해제됨"
}
}
}
}
'Coding > Android(kotlin)' 카테고리의 다른 글
Spinner (0) | 2022.02.08 |
---|---|
Context menu (0) | 2022.02.08 |
라디오 버튼 (0) | 2022.02.08 |
버튼(Button) 클릭시 alert dialog띄우기 (0) | 2022.02.08 |
버튼(Button) 클릭시 시간출력 이벤트 만들기 (0) | 2022.02.08 |