728x90
한빛아카데미 Android Studio를 활용한 안드로이드 프로그래밍 6판
p.407 직접 풀어보기 10-2
[실습 10-2]의 투표 결과 화면에서 가장 많은 표를 받은 그림과 그 제목을 화면에 보여준다.
Integer imageFileId[] = {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}
HINT
- result.xml과 ResultActivity.java 파일만 수정하여 작성한다.
- 레이팅바를 작은 모양으로 변경한다.
- 이미지 파일의 아이디를 저장할 배열을 만든다.
코드
MainActivity.java
package com.cookandroid.test;
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);
setTitle("명화 선호도 투표");
final int voteCount[] = new int[9];
for (int i = 0; i < 9; i++)
voteCount[i] = 0;
// 9개의 이미지 버튼 객체 배열
ImageView image[] = new ImageView[9];
// 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};
final String imgName[] = {"독서하는 소녀", "꽃장식 모자 소녀", "부채를 든 소녀",
"이레느깡 단 베르양", "잠자는 소녀", "테라스의 두 자매",
"피아노 레슨", "피아노 앞의 소녀들", "해변에서"};
for (int i = 0; i < imageId.length; i++) {
final int index = i; // 꼭 필요함
image[index] = findViewById(imageId[index]);
image[index].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
voteCount[index]++;
Toast.makeText(getApplicationContext(),
imgName[index] + ": 총 " + voteCount[index] + " 표",
Toast.LENGTH_SHORT).show();
}
});
}
Button btnFinish = (Button) findViewById(R.id.btnResult);
btnFinish.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra("VoteCount", voteCount);
intent.putExtra("ImageName", imgName);
startActivity(intent);
}
});
}
}
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
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:text="투표 종료" />
</LinearLayout>
ResultActivity.java
package com.cookandroid.test;
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;
import androidx.appcompat.app.AppCompatActivity;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle("투표 결과");
Intent intent = getIntent();
int[] voteResult = intent.getIntArrayExtra("VoteCount");
String[] imageName = intent.getStringArrayExtra("ImageName");
Integer imageFileId[] = {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};
TextView tvTop = findViewById(R.id.tvTop);
ImageView ivTop = findViewById(R.id.ivTop);
int maxEntry = 0;
for (int i = 1; i < voteResult.length; i++) {
if (voteResult[maxEntry] < voteResult[i])
maxEntry = i;
}
tvTop.setText(imageName[maxEntry]);
ivTop.setImageResource(imageFileId[maxEntry]);
TextView tv[] = new TextView[imageName.length];
RatingBar rbar[] = new RatingBar[imageName.length];
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] = (TextView) findViewById(tvID[i]);
rbar[i] = (RatingBar) findViewById(rbarID[i]);
}
for (int i = 0; i < voteResult.length; i++) {
tv[i].setText(imageName[i]);
rbar[i].setRating((float) voteResult[i]);
}
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
result.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:stretchColumns="0">
<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:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:layout_span="2"
android:scaleType="fitCenter"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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:attr/ratingBarStyleSmall"
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>
결과
728x90
'개발 > 책' 카테고리의 다른 글
[명품 웹 프로그래밍] 9장 Open Challenge (0) | 2021.11.22 |
---|---|
[안드로이드 프로그래밍] 10장 직접 풀어보기 10-1 (0) | 2021.11.19 |
[명품 웹 프로그래밍] 8장 Open Challenge (0) | 2021.11.15 |