감성 자동제어
[안드로이드 스튜디오]랜덤 숫자 발생시키는 방법
안녕하세요!! 오늘은 안드로이드 스튜디오 랜덤 숫자를 발생시키는 방법에 대해서 알아보도록 하겠습니다! 1~6까지의 랜덤번호를 만들어서 6개의 텍스트뷰에 대입해주도록 하겠습니다!
최종화면 미리보기
Activity_main.xml(layout)
1) "Button"하나와 "TextView" 6개를 만들고 각각 아이디를 만들어 주도록 합니다!
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
|
<?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"
android:layout_margin="10dp">
<Button
android:id="@+id/btn_random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="랜덤숫자 발생시키기"
android:textSize="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자1"
android:textSize="20dp"
android:id="@+id/random_text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자2"
android:textSize="20dp"
android:id="@+id/random_text2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자3"
android:textSize="20dp"
android:id="@+id/random_text3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자4"
android:textSize="20dp"
android:id="@+id/random_text4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자4"
android:textSize="20dp"
android:id="@+id/random_text5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="랜덤숫자4"
android:textSize="20dp"
android:id="@+id/random_text6"/>
</LinearLayout>
</LinearLayout>
|
cs |
MainActivity(Class)
1) Random random = new Random();
=> 버튼클릭시 랜덤을 발생시켜줍니다!
2) arr_random_num[i] = random.nextInt(6) + 1;
=> 랜던숫자를 "arr_random_num[]" 배열안에 넣어주도록 합니다!
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
|
public class MainActivity extends AppCompatActivity {
TextView textView[] = new TextView[6];
int arr_textview_id[] = {R.id.random_text1, R.id.random_text2, R.id.random_text3,
R.id.random_text4, R.id.random_text5, R.id.random_text6};
Button mbtn_random;
Integer arr_random_num[] = new Integer[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//텍스트 뷰 아이디 지정
for(int i = 0; i <arr_textview_id.length; i++){
final int index;
index = i;
textView[index] = findViewById(arr_textview_id[index]);
}
mbtn_random = findViewById(R.id.btn_random);
mbtn_random.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//버튼클릭시 램덤발생
Random random = new Random();
for(int i = 0; i < arr_random_num.length; i++){
//0~5까지 6개의 숫자중 랜덤발생 +1을 해줘서 1~6으로 바꿈
arr_random_num[i] = random.nextInt(6) + 1;
//랜덤숫자를 텍스트뷰에 대입
textView[i].setText(Integer.toString(arr_random_num[i]));
}
}
});
}
}
|
cs |
마무리
이상으로 랜덤 숫자를 발생시키는 방법에 대해 알아보았습니다. 긴글 읽어 주셔서 감사합니다!
'안드로이드 스튜디오' 카테고리의 다른 글
[안드로이드 스튜디오] 예외처리 try - catch문 사용방법!(계산기) (0) | 2020.12.22 |
---|---|
[안드로이드 스튜디오] 기초 완전 간단한 계산기 앱 만들기! (0) | 2020.11.30 |
[안드로이드 스튜디오] XML을 이용한 옵션메뉴 만들기! (0) | 2020.11.25 |
[안드로이드 스튜디오] 대화상자(dialog) 사용하는 방법-AlertDialog (0) | 2020.11.23 |
[안드로이드 스튜디오] 버튼 클릭시 이미지 변경!! (4) | 2020.11.20 |