대충벌레 블로그
article thumbnail
728x90
반응형

 안드로이드 로또어플 Java,xml 

 

로또

안드로이드를 배운 학생에게는 가장 쉬운 접근이 로또라고 생각이 든다

아마 안드로이드 뿐만 아니라 다른언어를 써서라도 한번쯤은 만들거나 생각해봤을거 같다.

 

XML 기본틀

XML을 어떻게 수정하든 본인의 입맛에 맛게 수정해도 된다.

필자는 정말 간단하게 생성하는  버튼과 초기화 버튼을 주엇다.

더보기
기본 틀 이미지
번호 생성시 이미지

입력하는곳에 1~5까지만 입력하면 번호가 생기고 그외 숫자는 입력하면 다시 입력해달라고 했다.

이곳에 찾아온 사람들은 소스가 가장 필요할테니 긴말없이 올리겠다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
 
    <LinearLayout
        android:id="@+id/layoutLotto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <TextView
             android:layout_gravity="center"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="로또 번호 생성기"
             android:textStyle="bold"
             android:textSize="25sp"/>
 
    <TableLayout
        android:stretchColumns="*"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        >
 
        <TableRow>
 
            <TextView
                android:text="생성할 갯수를 입력하세요."
                android:textSize="20sp"/>
 
            <EditText
                android:id="@+id/edtCount"
                android:inputType="number"
                android:hint="입력하는곳."
                />
 
        </TableRow>
 
        <TableRow>
 
        <Button
            android:id="@+id/btnMake"
            android:text="번호 생성하기"
            android:textSize="20sp"
            />
 
            <Button
                android:id="@+id/btnClear"
                android:text="초기화"
                android:textSize="20sp"
                />
 
        </TableRow>
 
    </TableLayout>
 
    <TextView
        android:id="@+id/txtCount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="생성 갯수 안내 or 안내메시지"
        android:visibility="invisible"
        android:textSize="20sp"/>
 
    <TextView
        android:id="@+id/txtCheck"
        android:padding="10dp"
        android:text="생성된 번호를 확인하세요."
        android:textSize="17sp"
        android:gravity="center"
        android:visibility="invisible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 
    <TableLayout
        android:id="@+id/tbLayout"
        android:visibility="invisible"
        android:shrinkColumns="*"
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
 
        <TableRow
            android:visibility="invisible"
            android:id="@+id/row1"
            >
 
            <Button android:id="@+id/btn01" />
 
            <Button android:id="@+id/btn02" />
 
            <Button android:id="@+id/btn03" />
 
            <Button android:id="@+id/btn04" />
 
            <Button android:id="@+id/btn05" />
 
            <Button android:id="@+id/btn06" />
 
        </TableRow>
 
        <TableRow
            android:visibility="invisible"
            android:id="@+id/row2">
 
            <Button
                android:id="@+id/btn11"/>
            <Button
                android:id="@+id/btn12"/>
            <Button
                android:id="@+id/btn13"/>
            <Button
                android:id="@+id/btn14"/>
            <Button
                android:id="@+id/btn15"/>
            <Button
                android:id="@+id/btn16"/>
 
        </TableRow>
 
        <TableRow
            android:visibility="invisible"
            android:id="@+id/row3">
 
            <Button
                android:id="@+id/btn21"/>
            <Button
                android:id="@+id/btn22"/>
            <Button
                android:id="@+id/btn23"/>
            <Button
                android:id="@+id/btn24"/>
            <Button
                android:id="@+id/btn25"/>
            <Button
                android:id="@+id/btn26"/>
 
        </TableRow>
 
        <TableRow
            android:visibility="invisible"
            android:id="@+id/row4">
 
            <Button
                android:id="@+id/btn31"/>
            <Button
                android:id="@+id/btn32"/>
            <Button
                android:id="@+id/btn33"/>
            <Button
                android:id="@+id/btn34"/>
            <Button
                android:id="@+id/btn35"/>
            <Button
                android:id="@+id/btn36"/>
 
        </TableRow>
 
        <TableRow
            android:visibility="invisible"
            android:id="@+id/row5">
 
            <Button
                android:id="@+id/btn41"/>
            <Button
                android:id="@+id/btn42"/>
            <Button
                android:id="@+id/btn43"/>
            <Button
                android:id="@+id/btn44"/>
            <Button
                android:id="@+id/btn45"/>
            <Button
                android:id="@+id/btn46"/>
        </TableRow>
 
    </TableLayout>
                </LinearLayout>
</LinearLayout>
cs

 

 

Java 소스

기본틀은 마무리 됬으니 Java소스는 부분적으로 알아보겠다

EditText edtCount;
Button btnMake,btnClear;
TextView txtCount,txtCheck;
TableLayout tblayout;
String sc;
TableRow[] trow=new TableRow[5];

int idNumber[][]={ {R.id.btn01,R.id.btn02,R.id.btn03,R.id.btn04,R.id.btn05,R.id.btn06},
            {R.id.btn11,R.id.btn12,R.id.btn13,R.id.btn14,R.id.btn15,R.id.btn16},
            {R.id.btn21,R.id.btn22,R.id.btn23,R.id.btn24,R.id.btn25,R.id.btn26},
            {R.id.btn31,R.id.btn32,R.id.btn33,R.id.btn34,R.id.btn35,R.id.btn36},
            {R.id.btn41,R.id.btn42,R.id.btn43,R.id.btn44,R.id.btn45,R.id.btn46}
    }; //버튼에 번호를 기록
Integer[] rows={R.id.row1,R.id.row2,R.id.row3,R.id.row4,R.id.row5};

int lottoNums[] = new int[6]; //로또번호 6개 저장할 공간

기본 선언 부분이다

btnMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sc=edtCount.getText().toString().trim();
                for (int a=0;a<trow.length;a++){
                    trow[a]=findViewById(rows[a]);
                    trow[a].setVisibility(View.INVISIBLE);
                }
                if(sc.trim().equals("")){
                    Toast.makeText(getApplicationContext(),"제대로 입력되지 않았습니다.",Toast.LENGTH_SHORT).show();

                }else if(0<Integer.parseInt(sc) && Integer.parseInt(sc)<=5 ) {
                    Toast.makeText(getApplicationContext(), "Lotto 번호를 생성하였습니다.", Toast.LENGTH_SHORT).show();
                    txtCount.setText(sc + "개 생성되었습니다.");
                    txtCount.setVisibility(View.VISIBLE);
                    txtCheck.setText("생성된 번호를 확인하세요.");
                    txtCheck.setVisibility(View.VISIBLE);
                    tblayout.setVisibility(View.VISIBLE);

                    for (int i = 0; i < Integer.parseInt(sc); i++) {

                        init(); //로또번호초기화
                        makeLotto(); // 로또번호 생성
                        sort(); // 로또번호 정렬
                        //trow[i]=findViewById(rows[i]);
                        trow[i].setVisibility(View.VISIBLE);

                        for (int j = 0; j < idNumber[0].length; j++) {
                            Button lottoNum = findViewById(idNumber[i][j]);
                            String str = lottoNums[j] + "";
                            lottoNum.setText(str);
                        }
                    }
                }
                else {
                    Toast.makeText(getApplicationContext(),"다시 입력해주세요.",Toast.LENGTH_SHORT).show();
                    txtCount.setText("잘못 입력하였습니다.");
                    txtCheck.setText("갯수는 최소1개 최대 5개 입니다.");
                    txtCount.setVisibility(View.VISIBLE);
                    txtCheck.setVisibility(View.VISIBLE);
                }
            }
        });

생성하기 버튼을 눌렀을때의 이벤트리스너 이다.

입력한 값을 받아와서 열을 초기화 하고 그 수만큼 보여주게 했다.

공부하면서 만드는거라 코드가 많이 더럽다. 양해 부탁드린다.

 

btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtCheck.setVisibility(View.INVISIBLE);
                txtCount.setVisibility(View.INVISIBLE);
                tblayout.setVisibility(View.INVISIBLE);
                edtCount.setText("");

            }
        });

초기화 버튼을 눌렀을때의 코드이다. 특별한거 없이 View를 안보이게 해주엇고 값을받는 부분은 아무것도없게 초기화.

setText("");를 하게되면 기본 xml에서 주었던 hint가 나오게 된다.

 

private void init(){
        for(int i=0;i<lottoNums.length;i++){
            lottoNums[i]=0;
        }
    }

init 메소드는 로또배열안에 숫자들은 0으로 다 초기화 한다는 뜻이다.

 

private void sort(){
        int k;
        for(int i=0;i<5;i++){
            for(int j=i;j<6;j++){
                if(lottoNums[i]>lottoNums[j]){
                    k=lottoNums[i];
                    lottoNums[i]=lottoNums[j];
                    lottoNums[j]=k;
                }
            }
        }
    } //sort 메소드 끝

sort 메소드는 배열에 있는 숫자들을 보기좋게 보기 편하게 정렬하는 메소드이다. 

반복문으로 돌리면서 어떤값이 큰지 비교하고있다.

 

private void makeLotto(){
        int n,m;
        for(int i=0;i<6;){
            m=(int)(Math.random()*45+1);
            for(n=0;n<i;n++){
                if(lottoNums[n]==m){
                    break;
                }
            }
            if(n==i){
                lottoNums[i]=m;
                i++;
            }
        }
    }

가장 중요한 로또번호 만드는 메소드이다. Math.random()함수로 랜덤값을 받아오고 넣어준다.

개발자의 차이는 여기서 많이  갈리는데 알고리즘을 어떻게 짜냐의 차이다 되게 간단하게 짜여있어서 만들어서 돌려보면 그냥 딱 45까지의 숫자중 아무거나 출력해준다. 

 

마무리

로또 어플을 만들면서 힘들거나  막히는부분을 댓글로 달아줬으면 좋겠고 저기에서 끝나는게 아닌

로또번호생성 메소드를 좀더 가꾸거나 원하는숫자는 받아서 포함할수있도록 하거나 싫어하는 숫자는 빼거나

그런식으로 하나 하나 추가해나가면서 더 좋고 효율적인 앱을 만들었으면 좋겠다.

다만 지금 마켓시장은 로또어플이 많으니 진입하기는 힘들것이다.

 

 

관련글

1.로또어플 다운받으러 가기 >> https://play.google.com/store/apps/details?id=com.inthegun.lotto

 

 
 

 

읽어주셔서 감사합니다.

공감,댓글 달아주시면 감사하겠습니다.

 

반응형
profile

대충벌레 블로그

@대충벌레

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!