불로구

안드로이드 스튜디오 - 로또 추첨기 만들기 본문

프로그래밍/코틀린 안드로이드

안드로이드 스튜디오 - 로또 추첨기 만들기

맹이맹이 2021. 3. 22. 02:32
반응형

 

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import org.w3c.dom.Text

class MainActivity : AppCompatActivity() {

    private val clearButton : Button by lazy{
        findViewById<Button>(R.id.clearButton)
    }
    private val addButton : Button by lazy{
        findViewById<Button>(R.id.addButton)
    }
    private val runButton : Button by lazy{
        findViewById<Button>(R.id.runButton)
    }

    private val numberPicker : NumberPicker by lazy{
        findViewById<NumberPicker>(R.id.numberPicker)
    }
    private val numberTextViewList : List<TextView> by lazy{
        listOf<TextView>(
            findViewById<TextView>(R.id.Number1),
            findViewById<TextView>(R.id.Number2),
            findViewById<TextView>(R.id.Number3),
            findViewById<TextView>(R.id.Number4),
            findViewById<TextView>(R.id.Number5),
            findViewById<TextView>(R.id.Number6)
        )
    }
    private var didRun = false
    private val pickNumberSet = hashSetOf<Int>()

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

        numberPicker.minValue = 1
        numberPicker.maxValue = 45

        initRunButton()
        initAddButton()
        initClearButton()
    }

    private fun initRunButton() : Unit{
        runButton.setOnClickListener{
            val list = getRandomNumber()
            didRun = true
            list.forEachIndexed{ i, number ->
                val textView = numberTextViewList[i]
                textView.text = number.toString()
                textView.isVisible = true
                setNumberBackground(number, textView)
            }
            Log.d("MainActivity", list.toString())
        }
    }

    private fun initAddButton() : Unit{
        addButton.setOnClickListener{
            if(didRun){
                Toast.makeText(this,"초기화 후 실행바랍니다.", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if(pickNumberSet.size >= 5){
                Toast.makeText(this,"번호는 5개까지만 선택 가능합니다.", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if(pickNumberSet.contains(numberPicker.value)){
                Toast.makeText(this,"이미 선택한 번호입니다.", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val textView = numberTextViewList[pickNumberSet.size]
            textView.isVisible = true
            textView.text = numberPicker.value.toString()

            setNumberBackground(numberPicker.value, textView)

            pickNumberSet.add(numberPicker.value)
        }
    }

    private fun setNumberBackground(number:Int, textView:TextView){
        when(number){
            in 1..10 -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_yellow)
            in 11..20 -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_blue)
            in 21..30 -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_red)
            in 31..40 -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_gray)
            else -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_green)
        }
    }

    private fun initClearButton(){
        clearButton.setOnClickListener {
            pickNumberSet.clear()
            numberTextViewList.forEach{
                it.isVisible = false
            }
            didRun = false
        }
    }

    private fun getRandomNumber() : List<Int>{
        val numberList = mutableListOf<Int>()
            .apply{
                for(i in 1..45){
                    if(pickNumberSet.contains((i))){
                        continue
                    }
                    this.add(i)
                }
            }
        numberList.shuffle() // List 섞어주기
        val newList = pickNumberSet.toList() + numberList.subList(0,6 - pickNumberSet.size) //numberList의 0~6번째 까지 숫자를 추출해서 넣어줌
        return newList.sorted()
    }
}

activity_main

<?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">

    <NumberPicker
        android:id="@+id/numberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="16dp"
        android:text="@string/addNumber"
        app:layout_constraintEnd_toStartOf="@id/clearButton"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/numberPicker" />

    <Button
        android:id="@+id/clearButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clearButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/addButton"
        app:layout_constraintTop_toTopOf="@id/addButton" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/addButton">

        <TextView
            android:id="@+id/Number1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_blue"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/Number2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_yellow"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/Number3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_red"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/Number4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_gray"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/Number5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_green"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/Number6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@drawable/circle_blue"
            android:textSize="18sp"
            android:textStyle="bold"
            android:visibility="gone"
            tools:visibility="visible" />
    </LinearLayout>

    <Button
        android:id="@+id/runButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/run"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

circle_blue.xml   ( 색상은 마음대로)

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <solid
        android:color="#5FB5DB"/>
    <size
        android:width="44dp"
        android:height="44dp"/>
</shape>

 

반응형
Comments