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
- Javscript
- JS
- RecyclerView
- stylesheet
- nodejs
- JavaScript
- 랜덤번호
- Linux
- 코틀린
- Android
- 랜덤넘버
- Kotlin
- array
- scrollview
- SpringBoot
- fragment
- button
- TextView
- 리액트
- 안드로이드
- 자바스크립트
- React
- Java
- GoogleMaps
- 구글맵스
- Hook
- npm
- TypeScript
- 오버라이딩
- 스프링부트
Archives
- Today
- Total
타닥타닥 개발자의 일상
kotlin으로 체크박스 만들기, 체크 여부 텍스트뷰에 연결하기 checkbox, Textview 본문
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/checkBox"
android:text="CheckBox"
android:layout_width="153dp"
android:layout_height="64dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="432dp"
/>
<TextView
android:id="@+id/textView"
android:layout_width="180dp"
android:layout_height="91dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.131"
app:layout_constraintTop_toBottomOf="@+id/checkBox"/>
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.sample12"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
추가된 부분
buildFeatures{
viewBinding true
}
MainActivity.kt
package com.example.sample12
import android.os.Bundle
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.sample12.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/* 첫번재 방법.
build.gradle 입력내용 이용하여 binding 사용하기.
setContentView(binding.root)
binding.checkBox.setOnCheckedChangeListener(checklistener) */
/* 두번째 방법.
현재 setContentView(binding.root)가 주석 처리 돼서 22~34줄 까지만 실행된다.*/
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 = "체크 해제됨"
}
}
}
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 = "체크 해제됨"
}
}
}
}
}
}
결과화면
Comments