Android - 로또번호 추첨기(3)

Code_Alpacat·2021년 11월 17일

안드로이드 기초

목록 보기
16/18

이번 코드까지는 업로드하고, 블로그에는 새로 사용해본 기능들이나 배운점들을 기록할 예정이다. 점점 갈수록 여러 기능들을 함수로 나눠 조립하는게 재밌지만, 그만큼 기록할 양이 방대해서 알고리즘을 위주로 블로그에 작성하고 왠만하면, 새로 배우는 이론과 기능들을 중점적으로 설명할 것이다.

  • 처음 사용해본 기능들을 나열해봤다.
  • visibility
  • forEach
  • numberPicker
  • Constarint Layout
  • drawble을 이용한 background 꾸미기
  • 예외처리
  • ContextCompat.getDrawble(this, 백그라운드 id)

여러 기능들을 함수단위로 만들어 유기적으로 연결하는 방법을 배우고나니 어렵지만 즐거웠다.

XML 레이아웃

<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="번호추가"
        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:layout_marginStart="16dp"
        android:text="초기화"
        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/textView1"
            android:background="@drawable/circle_blue"
            android:gravity="center"
            android:visibility="gone"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            tools:visibility="visible"
            android:text="1"
            android:textSize="20sp"
            android:textStyle="bold" />

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

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

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

        <TextView
            android:id="@+id/textView5"
            android:background="@drawable/circle_green"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_margin="5dp"
            tools:visibility="visible"
            android:textColor="@color/white"
            android:text="1"
            android:textSize="20sp"
            android:textStyle="bold" />
        <TextView
            android:background="@drawable/circle_blue"
            android:gravity="center"
            android:id="@+id/textView6"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:visibility="gone"
            tools:visibility="visible"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="1"
            android:textSize="20sp"
            android:textStyle="bold" />
    </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="자동생성시"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin

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.textView1),
            findViewById<TextView>(R.id.textView2),
            findViewById<TextView>(R.id.textView3),
            findViewById<TextView>(R.id.textView4),
            findViewById<TextView>(R.id.textView5),
            findViewById<TextView>(R.id.textView6),
        )
    }

    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() {

        runButton.setOnClickListener{
            val list = getRandomNumber()
            didRun = true

            list.forEachIndexed{ index, number ->
                val textView = numberTextViewList[index]

                textView.text = number.toString()
                textView.isVisible = true

                setNumberBackground(number, textView)

            }


        }
    }
    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()

        val newList = pickNumberSet.toList() + numberList.subList(0, 6 - pickNumberSet.size) //어디까지 자를 것인지 정함.

        return newList.sorted()

    }

    private fun initAddButton(){
        addButton.setOnClickListener{
            if(didRun) {
                Toast.makeText(this, "초기화 후에 실행해주세요.", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if(pickNumberSet.size >= 6) {
                Toast.makeText(this, "번호는 6개까지만 입력가능합니다.", 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_green)
            else -> textView.background = ContextCompat.getDrawable(this, R.drawable.circle_green)
        }

    }

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

            didRun = false

        }

    }


}
profile
In the future, I'm never gonna regret, cuz I've been trying my best for every single moment.

0개의 댓글