상세 컨텐츠

본문 제목

[안드로이드 코틀린] TextView 로또 번호 생성하는 방법

안드로이드

by aries574 2022. 8. 1. 14:51

본문


이번 시간에는 랜덤으로 숫자를 만들어 로또 번호 만드는 방법을 알아보겠습니다. 


목차

1. 실행 화면
2. 메인 화면 activity_main.xml
3. 메인 코드 MainActivity.kt


1. 실행 화면

 


2. 메인 화면 activity_main.xml

- 설명 -

1. 로또 번호 보여줄 TextView

2. 로또 번호 생성할 Button

<RelativeLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/lottoText"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FAEBFF"
        android:layout_above="@id/lottoBtn"
        />

    <Button
        android:id="@+id/lottoBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:text="로또 생성"/>
</RelativeLayout>

 


3. 메인 코드 MainActivity.kt

- 설명 -

1. val lottoSet: HashSet<String>

  숫자 중복 방지을 위한 HashSet

2. Math.random() * 46

  0 ~ 45까지 랜덤 숫자 발생

3. if(num != 0)

  0를 제외한 숫자만 체크

class MainActivity : AppCompatActivity(){

    lateinit var lottoText: TextView

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

        //객체 생성
        lottoText = findViewById(R.id.lottoText)
        val lottoBtn: Button = findViewById(R.id.lottoBtn)

        //로또 번호 생성
        lottoBtn.setOnClickListener {

            //로또 텍스트뷰 값 있으면 초기화
            if(lottoText.text.toString() != ""){
                lottoText.text = ""
            }

            //로또 생성
            createLotto()
        }
    }//onCreate

    private fun createLotto(){

        //Set, List 생성
        val lottoSet: HashSet<String> = HashSet()
        val list: ArrayList<String> = ArrayList()

        //7개 생성할때까지 반복
        while(lottoSet.size < 7){
            val num: Int = (Math.random() * 46 as Int).toInt()

            //숫자 0이 아니면 등록
            if(num != 0){
                lottoSet.add(num.toString())
            }
        }

        //list 담기
        list.addAll(lottoSet)

        for((index, item) in list.withIndex()){

            //0 ~ 6까지는 첫 째줄
            if(index < 6){
                lottoText.append("$item   ")

            }else{ // 나머지 숫자는 줄바꿈
                lottoText.append("\n 보너스 번호 $item")
            }
        }

        //초기화
        lottoSet.clear()
        list.clear()
    }

}

2022.07.31 - [안드로이드] - [안드로이드 코틀린] 텍스트뷰 TextView 동적으로 생성하는 방법

 

[안드로이드 코틀린] 텍스트뷰 TextView 동적으로 생성하는 방법

이번 시간에는 텍스트뷰(TextView)를 코드를 통해 동적으로 생성하는 방법을 알아보겠습니다. 목차 1. 실행 화면 2. 메인 화면 activity_main.xml 3. 메인 코드 MainActivity.kt 1. 실행 화면 2. 메인..

aries574.tistory.com

2022.07.30 - [안드로이드] - [안드로이드 코틀린] TimePicker 내가 선택한 알람 시간 텍스트뷰에 보여주는 방법

 

[안드로이드 코틀린] TimePicker 내가 선택한 알람 시간 텍스트뷰에 보여주는 방법

이번 시간에는 TimePicker를 이용해서 시간을 선택하고 TextView에 보여주는 방법에 대하여 알아보겠습니다. 목차 1. 실행 화면 2. 메인 화면 activity_main.xml 3. 메인 코드 MainActivity 1. 실행 화..

aries574.tistory.com

 

2022.07.28 - [안드로이드] - [안드로이드 코틀린] Notification 간단한 알림 띄우기

 

[안드로이드 코틀린] Notification 간단한 알림 띄우기

이번 시간에는 Notification을 이용해서 상태 표시줄에 알림을 띄어주는 방법을 알아보겠습니다. 목차 1. 실행 화면 2. Notification 클래스 만들기 3. 메인 화면 구현 activity_main.xml 4. 메인 코..

aries574.tistory.com

 

반응형

관련글 더보기

댓글 영역