728x90
반응형
📃랜덤 숫자 게임
📝activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="게임 시작하기"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_01"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#ff9999"
android:gravity="center"
android:textSize="40sp" />
<TextView
android:id="@+id/tv_02"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#9999ff"
android:gravity="center"
android:textSize="40sp" />
<TextView
android:id="@+id/tv_03"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#99ffff"
android:gravity="center"
android:textSize="40sp" />
</LinearLayout>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="멈추기"
android:textSize="20dp" />
</LinearLayout>
📝MainActivity.java
package com.project.review02;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv_01;
TextView tv_02;
TextView tv_03;
Button btn_stop;
int num1;
int num2;
int num3;
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
num1 = getRandomNum();
num2 = getRandomNum();
num3 = getRandomNum();
if(msg.what == 1) {
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeMessages(1);
}
});
tv_01.setText(num1+"");
tv_02.setText(num2+"");
tv_03.setText(num3+"");
sendEmptyMessageDelayed(1, 50);
}
}
};
public int getRandomNum() {
int num = (int)(Math.random()*10)+1;
return num;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_01 = findViewById(R.id.tv_01);
tv_02 = findViewById(R.id.tv_02);
tv_03 = findViewById(R.id.tv_03);
btn_stop = findViewById(R.id.btn_stop);
findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(1);
}
});
}
}
728x90
반응형
'Project > Study | etc' 카테고리의 다른 글
구구단 게임 (0) | 2022.08.11 |
---|---|
Team Project - 계산기 프로그램 (0) | 2022.07.01 |
상품 구매 프로그램 (0) | 2022.06.29 |
성적 관리 프로그램 + 설계, 메소드 구현 (0) | 2022.06.28 |
랜덤 카드 추출 게임 (0) | 2022.06.26 |
댓글