PROGRAMMING

이상형월드컵(ver. teacher) 본문

Android

이상형월드컵(ver. teacher)

Raccoon2125 2020. 10. 30. 12:49

1. custom으로 randomarray 클래스 생성

package com.example.imageviewapp;

import android.widget.Toast;

import java.util.Random;

public class RandomValues {
    int randomArray[];
    public RandomValues(int arraySize){
        Random random = new Random();
        int array_length=7;
        randomArray = new int[array_length];

        for(int i=0; i<arraySize; i++) {
            randomArray[i] = random.nextInt(arraySize);
            for (int j = 0; j < i; j++) {
                if (randomArray[i] == randomArray[j]) {
                    i--;
                }
            }
        }
    }

    public int[] getRandomArray() {
        return randomArray;
    }

    public void setRandomArray(int[] randomArray) {
        this.randomArray = randomArray;
    }
}

2. MainActivity.java

package com.example.imageviewapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioTop, radioBottom;
    ImageView imageView;
    ImageView imageView2;
    TextView txtView;

    RandomValues randomArray;

    String[] titles = {
            "그림0",
            "그림1",
            "그림2",
            "그림3",
    } ;

    Integer[] images = {
            R.drawable.fig0,
            R.drawable.fig1,
            R.drawable.fig2,
            R.drawable.fig3,
    };

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

        randomArray = new RandomValues(4);
        randomArray.getRandomArray();
        txtView = findViewById(R.id.textView);
        imageView = findViewById(R.id.imageView);
        imageView.setImageResource(images[randomArray.randomArray[0]]);
        imageView2 = findViewById(R.id.imageView2);
        imageView2.setImageResource(images[randomArray.randomArray[1]]);
        radioTop = findViewById(R.id.radioTop);
        radioBottom = findViewById(R.id.radioBottom);
        radioGroup = findViewById(R.id.radioGroup);
        txtView.setText("이상형 월드컵 4강 - 1");

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            int stage = 0;
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton select = (RadioButton)findViewById(checkedId);
                switch (select.getId()){
                    case R.id.radioTop:
                        radioTop.setChecked(false);
                        showImageView(stage, 0);
                        break;
                    case R.id.radioBottom:
                        radioBottom.setChecked(false);
                        showImageView(stage, 1);

                        break;
                }
                stage+=1;
            }
        });
    }
    
    public void showImageView(int stage, int select){
            int[] tmpArray = randomArray.getRandomArray();
            int selectNum2 = 4;
            if(stage == 0 ){
                txtView.setText("이상형 월드컵 4강 - 2");
                tmpArray[stage+selectNum2] = tmpArray[2*stage+select];
                randomArray.setRandomArray(tmpArray);
                imageView.setImageResource(images[tmpArray[2*(stage+1)]]);
                imageView2.setImageResource(images[tmpArray[2*(stage+1)+1]]);
            }
            if(stage == 1){
                txtView.setText("이상형 월드컵 결승");
                tmpArray[stage+selectNum2] = tmpArray[2*stage+select];
                randomArray.setRandomArray(tmpArray);
                imageView.setImageResource(images[tmpArray[2*(stage+1)]]);
                imageView2.setImageResource(images[tmpArray[2*(stage+1)+1]]);

            }
            else if (stage == 2){
                tmpArray[stage+selectNum2] = tmpArray[2*stage+select];
                randomArray.setRandomArray(tmpArray);
                //imageView.setImageResource(images[tmpArray[2*(stage+1)]]);
                //imageView2.setImageResource(images[tmpArray[2*(stage+1)+1]]);
                new AlertDialog.Builder(this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("우승")
                        .setMessage(String.format("우승 : %s",titles[tmpArray[2*stage+select]]))
                        .setNeutralButton("닫기", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        })
                        .show();
            }
            else{

            }
            Toast.makeText(getApplicationContext(),titles[tmpArray[2*stage+select]],Toast.LENGTH_SHORT).show();
    }
}


3. 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="23sp"
        android:layout_gravity="center_horizontal"
        android:text="이상형월드컵" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </FrameLayout>

        </LinearLayout>

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radioTop"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <RadioButton
                android:id="@+id/radioBottom"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1" />

        </RadioGroup>

    </LinearLayout>

</LinearLayout>

'Android' 카테고리의 다른 글

버튼-백그라운드컬러  (0) 2020.11.03
어플 접속화면(feat. 이상형월드컵)  (0) 2020.10.30
AlertDialog  (0) 2020.10.30
PopUp  (0) 2020.10.30
Custom Dialog (로그인)  (0) 2020.10.30
Comments