타닥타닥 개발자의 일상

kotlin으로 체크박스 만들기, 체크 여부 텍스트뷰에 연결하기 checkbox, Textview 본문

카테고리 없음

kotlin으로 체크박스 만들기, 체크 여부 텍스트뷰에 연결하기 checkbox, Textview

NomadHaven 2022. 2. 7. 01:04
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