728x90
반응형
실습 10-2에서 에러나는 부분이 있어서 코드를 수정해서 올렸다.
일단 프로젝트 위치 사진이다.
MainActivity.kt
package com.example.project10_2
import android.content.Intent
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "영화 선호도 투표"
var voteCount = IntArray(9)
for(i in 0..8)
voteCount[i] = 0
var image = arrayOfNulls<ImageView>(9)
var imageId = arrayOf(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)
var imgName = arrayOf("독서하는 소녀", "꽃장식 모자 소녀",
"부채를 든 소녀", "이레느깡 단 베르망", "잠자는 소녀", "테라스의 두 자매", "피아노 레슨",
"피아노 앞의 소녀들", "해변에서")
for(i in imageId.indices){
image[i] = findViewById<ImageView>(imageId[i])
image[i]!!.setOnClickListener {
voteCount[i]++
Toast.makeText(applicationContext, imgName[i] + ": 총 " + voteCount[i] + " 표",Toast.LENGTH_SHORT).show()
}
}
var btnFinish = findViewById<Button>(R.id.btnResult)
btnFinish.setOnClickListener {
var intent = Intent(applicationContext, ResultActivity::class.java)
intent.putExtra("VoteCount", voteCount)
intent.putExtra("ImageName", imgName)
startActivity(intent)
}
}
}
ResultActivity.kt
package com.example.project10_2
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class ResultActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.result)
title = "투표 결과"
val intent = intent
val voteResult = intent.getIntArrayExtra("VoteCount")!!
val imageName = intent.getStringArrayExtra("ImageName")!!
val imageFileId = arrayOf(
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
)
val tvTop = findViewById<TextView>(R.id.tvTop)
val ivTop = findViewById<ImageView>(R.id.ivTop)
var maxEntry = 0
for (i in 1 until voteResult.size) {
if (voteResult[maxEntry] < voteResult[i]) {
maxEntry = i
}
}
tvTop.text = imageName[maxEntry]
ivTop.setImageResource(imageFileId[maxEntry])
val tv = arrayOfNulls<TextView>(imageName.size)
val rbar = arrayOfNulls<RatingBar>(imageName.size)
val tvID = arrayOf(
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
)
val rbarID = arrayOf(
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 (i in 0 until voteResult.size) {
tv[i] = findViewById(tvID[i])
rbar[i] = findViewById(rbarID[i])
}
for (i in 0 until voteResult.size) {
tv[i]?.text = imageName[i]
rbar[i]?.rating = voteResult[i].toFloat()
}
val btnReturn = findViewById<Button>(R.id.btnReturn)
btnReturn.setOnClickListener {
finish()
}
}
}
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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
>
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic4" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv2"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic3" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv3"
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="wrap_content"
android:layout_weight="3">
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv4"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic4" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv5"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic5" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv6"
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="wrap_content"
android:layout_weight="3">
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv7"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic7" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv8"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pic8" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv9"
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="50dp"
android:text="투표 종료" />
</LinearLayout>
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
반응형
'Kotlin' 카테고리의 다른 글
[Kotlin을 활용한 안드로이드 프로그래밍] 연습문제 11-6 (0) | 2023.06.11 |
---|---|
[Kotlin을 활용한 안드로이드 프로그래밍] 연습문제 11-5 (1) | 2023.06.11 |
[Kotlin을 활용한 안드로이드 프로그래밍] 9-2 직접 풀기 (0) | 2023.05.27 |
[Kotlin을 활용한 안드로이드 프로그래밍] 시스템 폴더/파일목록 (0) | 2023.05.22 |
코틀린 에뮬레이터 or 디바이스 내에 폴더 생성 및 삭제 (1) | 2023.05.22 |