Toast: 랜덤 숫자 맞추기 게임
1. 레이아웃 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/game">
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="행운의 주인공이 되세요."
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#000"
android:gravity="center"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "선택1"
android:textSize="15sp"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dp"
android:background="@drawable/round_box"
android:layout_weight="1" />
<Button
android:id="@+id/btn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "선택2"
android:textSize="15sp"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dp"
android:background="@drawable/round_box"
android:layout_weight="1" />
<Button
android:id="@+id/btn03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "선택3"
android:textSize="15sp"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dp"
android:background="@drawable/round_box"
android:layout_weight="1" />
<Button
android:id="@+id/btn04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "선택4"
android:textSize="15sp"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dp"
android:background="@drawable/round_box"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="당첨 결과 : "
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ca1c1c"
android:gravity="center"
/>
</LinearLayout>
2. round_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#d941c5" />
<corners android:radius="20dp" />
</shape>
3. 실행 클래스 MainActivity.java
package com.progdv.app04_game;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn01, btn02, btn03, btn04;
TextView result;
int r = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn01 = (Button)findViewById(R.id.btn01);
btn02 = (Button)findViewById(R.id.btn02);
btn03 = (Button)findViewById(R.id.btn03);
btn04 = (Button)findViewById(R.id.btn04);
result = (TextView)findViewById(R.id.result);
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
btn04.setOnClickListener(this);
r = new Random().nextInt(4)+1;
}
@Override
public void onClick(View v) {
int n = 0;
String msg = null;
switch (v.getId()){
case R.id.btn01:
n = 1;
break;
case R.id.btn02:
n = 2;
break;
case R.id.btn03:
n = 3;
break;
case R.id.btn04:
n = 4;
break;
}
if(n == r){
msg = "축하합니다. 당첨되셨습니다.!!";
}else{
msg = "안타깝습니다. \n다음 기회에 다시 도전하세요.";
}
result.setText(msg);
}
}
'안드로이드' 카테고리의 다른 글
컨텍스트 메뉴 (contextmenu) (0) | 2016.04.28 |
---|---|
액션바 ActionBar_Items (0) | 2016.04.28 |
이벤트 처리: Toast 이벤트 기초 (0) | 2016.04.26 |
그리드 뷰 (GridView) (0) | 2016.04.26 |
AdapterView: Spinner (0) | 2016.04.26 |