2swan

Android 투표 앱 예제 본문

Programming/Android Example

Android 투표 앱 예제

2swan 2023. 8. 3. 12:54

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"
    tools:context=".MainActivity3"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">

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

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">

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


    <Button
        android:id="@+id/btnResult"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textSize="20sp"
        android:text="투표종료"/>


</LinearLayout>

 

sub.xml

<?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"
    tools:context=".MainActivity3_Sub">
<TableRow>
    <TextView
        android:id="@+id/tvTop"
        android:text="우승그림"
        android:textSize="15sp"
        android:layout_span="2"
        android:layout_gravity="center"
        />
</TableRow>

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

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

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

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

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

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

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

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

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

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

    <TableRow>
        <Button
            android:id="@+id/btnReturn"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_span="2"
            android:text="돌아가기" />
    </TableRow>

</TableLayout>

 

main.java

public class MainActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Button btnResult = findViewById(R.id.btnResult);
        //투표수 저장 배열
        int countVote[] = new int[9];
        ImageView image[] = new ImageView[9];  //이미지 뷰 저장 배열
        String ImageName[] = {
                "pic1","pic2","pic3",
                "pic4","pic5","pic6",
                "pic7","pic8","pic9"
        };
        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
        };


        for(int i=0; i<imageId.length; i++){
            final  int index = i;
            image[index] = findViewById(imageId[index]);
            image[index].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //투표수 증가 Toast 띄우기
                    countVote[index]++;
                    Toast.makeText(getApplicationContext(), ImageName[index] + " 총 "
                            + countVote[index] + " 표", Toast.LENGTH_LONG).show();
                }
            });
        }

        //투표종료
        btnResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity3.this,MainActivity3_Sub.class);
                intent.putExtra("countVote",countVote);
                intent.putExtra("ImageName",ImageName);
                startActivity(intent);
            }
        });
    }
}

 

sub.java

public class MainActivity3_Sub extends AppCompatActivity {

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

        setTitle("투표결과");
        Intent intent = getIntent();
        int[] voteResult = intent.getIntArrayExtra("countVote");
        String[] ImageResult = intent.getStringArrayExtra("ImageName");

        Integer imageFieldId[] = {
                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등 찾기
        int maxPos = 0;     //최대위치
        for (int i = 0; i < voteResult.length; i++) {
            if (voteResult[maxPos] < voteResult[i]) {
                maxPos = i;
            }
        }
        TextView tvTop = findViewById(R.id.tvTop);
        ImageView ivTop = findViewById(R.id.ivTop);
        tvTop.setText(ImageResult[maxPos] + "(" + voteResult[maxPos] + ")");    //1등 그림 이름과 투표수
        ivTop.setImageResource(imageFieldId[maxPos]);   //1등 이미지
        //9개의 TextView, RatingBar  객체 배열 생성
        TextView tv[] = new TextView[ImageResult.length];
        RatingBar rbar[] = new RatingBar[ImageResult.length];
        //9개의 TextView, RatingBar 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};

        //초기화
        for (int i = 0; i < voteResult.length; i++) {
            tv[i] = findViewById(tvID[i]);
            rbar[i] = findViewById(rbarID[i]);
        }

        //값 세팅
        for (int i = 0; i < voteResult.length; i++) {
            tv[i].setText(ImageResult[i]);
            rbar[i].setRating(voteResult[i]);
        }

        Button btnReturn = findViewById(R.id.btnReturn);
        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

결과 값