정보를 보호하는 대학생

<안드로이드 스터디 02> 체크박스를 누르면 토스트 띄우기 본문

개발/안드로이드 스터디

<안드로이드 스터디 02> 체크박스를 누르면 토스트 띄우기

정보를 보호하는 대학생 2022. 1. 6. 22:54

1. xml 코드

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="128dp"
        android:layout_marginTop="205dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="478dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.58"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <CheckBox
            android:id="@+id/checkA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="사과" />

        <CheckBox
            android:id="@+id/checkO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="오렌지" />

        <CheckBox
            android:id="@+id/checkB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="바나나" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

2. 코틀린 코드

package com.android.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CheckBox
import android.widget.Toast

class MainActivity : AppCompatActivity() {
//////////변수선언/////////////////
    lateinit var checkA : CheckBox
    lateinit var checkO : CheckBox
    lateinit var checkB : CheckBox

/////////////////////////////////

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


    checkA = findViewById(R.id.checkA)
        checkA.setOnCheckedChangeListener{buttonView, isChecked ->  //사과를 체크하면
            if(isChecked){   
                Toast.makeText(applicationContext, "사과", Toast.LENGTH_SHORT).show() //"사과" 토스트 띄움
            }

        }


     checkO = findViewById(R.id.checkO)

     checkO.setOnCheckedChangeListener { buttonView, isChecked ->
         if (isChecked) {
             Toast.makeText(applicationContext, "오렌지", Toast.LENGTH_SHORT).show()
         } }


        checkB = findViewById(R.id.checkB)

        checkB.setOnCheckedChangeListener{buttonView, isChecked->
            if(isChecked){
                Toast.makeText(applicationContext,"바나나", Toast.LENGTH_SHORT).show()
            }

        }


    }



}

3. 결과화면