본문 바로가기
Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판][Coding Challenge]주사위 게임 응용 - 음식 메뉴 추천하기(과제)

by starfish22 2022. 9. 21.
728x90

▶음식 메뉴 추천하기

Roll 버튼을 누를 시 랜덤으로 메뉴를 추천한다.

 

▶코드 작성

activity_main.xml

<?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:gravity="center"
    android:background="#000000">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt"
        android:textSize="100dp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:gravity="center" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="뭐 먹을지 고민되니?"
        android:textSize="30dp"
        android:textColor="#ffff00"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginBottom="10dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:orientation="vertical"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/question"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#ffff00"
            android:text="ROLL"
            android:textColor="#000000"
            android:textStyle="bold"
            android:textSize="25dp"
            android:layout_marginTop="10dp"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java

package kr.co.company.diceroll;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button button;
    ImageView imageView;
    TextView textView;

    int r, n = -1;
    String[] str = {"'한 식'", "'일 식'", "'중 식'", "'양 식'", "'분 식'", "'야 식'"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int[] arr = {R.drawable.koreanfood, R.drawable.japanesefood, R.drawable.chinesefood,
                R.drawable.westernfood, R.drawable.flourbasedfood, R.drawable.lsn};

        button = (Button) findViewById(R.id.btn);
        imageView = (ImageView) findViewById(R.id.img);
        textView = (TextView) findViewById(R.id.txt);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                r = (int) (Math.random() * 6);
                while (r == n) {
                    r = (int) (Math.random() * 6);
                }
                n = r;
                imageView.setImageResource(arr[r]);
                textView.setText(str[r]);
            }
        });
    }
}

 

▶실행 영상

 

 

728x90

댓글