본문 바로가기
App/Android Java

Android_Java(19) - MyIntent(2)

by SeleniumBindingProtein 2022. 2. 11.
728x90
반응형

<ResultActivity>

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv1"
            android:src="@drawable/pic1"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv2"
            android:src="@drawable/pic2"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv3"
            android:src="@drawable/pic3"/>
    </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv4"
            android:src="@drawable/pic4"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv5"
            android:src="@drawable/pic5"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv6"
            android:src="@drawable/pic6"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv7"
            android:src="@drawable/pic7"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv8"
            android:src="@drawable/pic8"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:id="@+id/iv9"
            android:src="@drawable/pic9"/>
    </LinearLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnResult"
        android:layout_weight="1"
        android:text="투표 종료"/>
</LinearLayout>
package kr.co.myintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
        setTitle("명화 선호도 투표");

        // 그림을 클릭할 때마다 투표수를 저장할 9개짜리 배열을 선언, 0으로 초기화
        final int[] voteCount = new int[9];
        for (int i=0; i<9; i++) {
            voteCount[i] = 0;
        }

        // 이미지뷰 위젯을 저장할 9개짜리 배열 선언
        ImageView[] imageViews = new ImageView[9];
        // 이미지뷰 위젯의 id를 저장할 배열 선언
        Integer[] imageId = {R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5,
                R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9};
        // 그림의 이름을 저장할 9개짜리 배열 선언
        final String[] imagName = { "독서하는 소녀", "꽃장식 소녀", "부채를 든 소녀", "이레느깡 단 베르양",
                "잠자는 소녀", "테라스의 두 자매", "피아노 레슨", "피아노 앞의 소녀들",
                "해변에서"};

        // 각 이미지뷰에 대한 클릭 이벤트 리스너 생성
        // 이미지뷰가 9개이므로 반복문 사용
        // 이미지를 클릭하면 각 이미지의 투표수가 증가하도록 설정
        // 이미지를 클릭할 때마다 해당 이미지 이름과 누적된 투표수를 토스트 메시지로 보여줌.

        for (int i = 0; i < imageId.length; i++) {
            final int index;        //꼭필요함
            index = i;

            imageViews[index] = findViewById(imageId[index]);
            imageViews[index].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //투표수 증가
                    voteCount[index]++;
                    Toast.makeText(getApplicationContext(),
                            imagName[index] +": 총 "+ voteCount[index] +" 표", Toast.LENGTH_SHORT).show();
                }
            });

        }


        //<투표 종료> 클릭 이벤트 리스너 생성
        //인텐트 생성, 인텐트에 투표수 배열과 그림 이름 배열 넣은 후 ResultActivity 호출
        Button btnFinish = findViewById(R.id.btnResult);
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //인텐트 객체에 실행하고 싶은 액티비티 클래스 이름 ResultActivity를 지정함
                Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
                intent.putExtra("VoteCount", voteCount);
                intent.putExtra("ImageName", imagName);

                //intent 객체에 기술된 액티비티를 시작함
                startActivity(intent);
            }
        });

    }
}

<MainActivity>

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:gravity="center_vertical"
    android:stretchColumns="0"
    tools:context=".ResultActivity">

    <TableRow>
        <TextView
            android:id="@+id/tvTop"
            android:layout_gravity="center"
            android:layout_span="2"
            android:text="## 우승 그림 : "
            android:textSize="15dp"/>
    </TableRow>

    <TableRow>
        <ImageView
            android:id="@+id/ivTop"
            android:scaleType="fitCenter"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            android:layout_span="2"
            android:src="@drawable/pic1"/>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv1"
            android:layout_gravity="center_vertical"
            android:text="명화1"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar1"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv2"
            android:layout_gravity="center_vertical"
            android:text="명화2"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar2"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv3"
            android:layout_gravity="center_vertical"
            android:text="명화3"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar3"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv4"
            android:layout_gravity="center_vertical"
            android:text="명화4"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar4"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv5"
            android:layout_gravity="center_vertical"
            android:text="명화5"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar5"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv6"
            android:layout_gravity="center_vertical"
            android:text="명화6"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar6"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv7"
            android:layout_gravity="center_vertical"
            android:text="명화7"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar7"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv8"
            android:layout_gravity="center_vertical"
            android:text="명화8"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar8"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <TextView
            android:id="@+id/tv9"
            android:layout_gravity="center_vertical"
            android:text="명화9"
            android:textSize="15dp"/>
        <RatingBar
            android:id="@+id/rbar9"
            style="?android:ratingBarStyleIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
        </RatingBar>
    </TableRow>

    <TableRow>
        <Button
            android:id="@+id/btnReturn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="돌아가기"/>
    </TableRow>
</TableLayout>
package kr.co.myintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
/*
    영화 투표 앱을 수정하시오.
        1) 투표 결과 화면에서 가장 많은 표를 받은 그림과 그 제목을 화면에 보여주시오.
            - activity_result.xml과 ResultActivity.java 파일 수정하여 작성
            - Rating Bar를 작은 모양으로 변경
            - 이미지 파일의 아이디를 저장할 배열 생성
 */
public class ResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
        setTitle("명화 선호도 투표 결과");

        // 메인 액티비티에서 보낸 인텐트 받고, 넘겨받은 투표 결과 배열과 그림 이름 배열 저장

        // 앞 액티비티(화면)에서 보낸 투표 결과 값을 받음.
        Intent intent = getIntent();
        int[] voteResult = intent.getIntArrayExtra("VoteCount");
        String[] imageName = intent.getStringArrayExtra("ImageName");

        Integer[] imageField = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
                                 R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
                                 R.drawable.pic7, R.drawable.pic8, R.drawable.pic9 };

        //1등 그림이름과 그림파일을 보여줌
        TextView textViewTop = findViewById(R.id.tvTop);
        ImageView imageViewTop = findViewById(R.id.ivTop);
        int maxEntry = 0;
        for(int i=1;i<voteResult.length;i++){
            if(voteResult[maxEntry] < voteResult[i]){
                maxEntry = i;
            }
        }

        textViewTop.setText(imageName[maxEntry]);
        imageViewTop.setImageResource(imageField[maxEntry]);


        // activity_result.xml의 텍스트뷰 9개, 레이팅 바 9개 위젯 변수 배열을 선언
        TextView[] textViews = new TextView[imageName.length];
        RatingBar[] ratingBars = new RatingBar[imageName.length];

        // 텍스트 뷰 id를 저장할 배열 변수, 레이팅바 id를 저장할 배열 변수를 선언
        Integer[] tvID = {R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5,
                R.id.tv6, R.id.tv7, R.id.tv8, R.id.tv9};
        Integer[] rbarId = {R.id.rbar1, R.id.rbar2, R.id.rbar3, R.id.rbar4, R.id.rbar5,
                R.id.rbar6, R.id.rbar7, R.id.rbar8, R.id.rbar9};

        // XML 파일의 텍스뷰와 레이팅바를 위젯 변수에 대입
        // 9개의 TextView, RatingBar 개체 연결하기
        for (int i = 0; i < voteResult.length; i++) {
            textViews[i] = findViewById(tvID[i]);
            ratingBars[i] = findViewById(rbarId[i]);
        }

        //텍스트뷰 위젯 변수에 넘겨받은 그림 이름 적용
        //레이팅바에는 넘겨받은 투표 결과를 적용
        for (int i = 0; i < voteResult.length; i++) {
            textViews[i].setText(imageName[i]);
            ratingBars[i].setRating((float) voteResult[i]);
        }

        //버튼을 클릭하면 ResultActivity를 종료. 즉 메인 액티비티로 돌아감.
        Button btnReturn = findViewById(R.id.btnReturn);
        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}

[결과 출력]

투표에서 가장 높은 투표 수를 받은 그림을 출력!!

00

 

728x90
반응형

'App > Android Java' 카테고리의 다른 글

Android_Java(21) - ImplicitIntent  (0) 2022.02.11
Android_Java(20) - MyIntent(3)  (0) 2022.02.11
Android_Java(18) - MyIntent  (0) 2022.02.10
Android_Java(17) - MyActivity(2)  (0) 2022.02.10
Android_Java(16) - MyActivity  (0) 2022.02.10

댓글