Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)
- 레이팅바를 이용한 그림 투표 인텐트
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="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:background="@drawable/button"
android:textColor="#E91E63"
android:textSize="20dp"
android:text="결과 보기" />
</LinearLayout>
------------------------------------------------------------------------------------
botton.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : button , Root Element : shape
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#8EC7F4" />
<stroke android:color="#ff0000ff" android:width="4dp" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners
android:radius="30dp"
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
</shape>
-------------------------------------------------------------------------------------
***** activity_result.xml **
--- res/layout 에서 ---> New --> XML --> Layout XML File
--- File : activity_result , Toot Tag : TableLayout
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<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: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: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:background="@drawable/button"
android:text="돌아가기" />
</TableRow>
</TableLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "명화 선호도 투표 (개선)"
supportActionBar!!.setDisplayShowHomeEnabled(true)
supportActionBar!!.setIcon(R.drawable.pici_icon)
val voteCount = IntArray(9)
for (i in 0..8)
voteCount[i] = 0
// 9개의 이미지 버튼 객체배열
val image = arrayOfNulls<ImageView>(9)
// 9개의 이미지버튼 ID 배열
val 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)
val imgName = arrayOf("독서하는 소녀", "꽃장식 모자 소녀", "부채를 든 소녀", "이레느깡 단 베르양", "잠자는 소녀", "테라스의 두 자매", "피아노 레슨", "피아노 앞의 소녀들", "해변에서")
for (i in imageId.indices) {
image[i] = findViewById<View>(imageId[i]) as ImageView
image[i]?.setOnClickListener(View.OnClickListener {
// 투표수 증가.
voteCount[i]++
Toast.makeText(applicationContext,
imgName[i] + ": 총 " + voteCount[i] + " 표",
Toast.LENGTH_SHORT).show()
})
}
val btnFinish = findViewById<View>(R.id.btnResult) as Button
btnFinish.setOnClickListener {
val intent = Intent(applicationContext,
ResultActivity::class.java)
intent.putExtra("VoteCount", voteCount)
intent.putExtra("ImageName", imgName)
startActivity(intent)
}
}
}
------------------------------------------------------------------------------------
***** SubActivity.kt **
-- New --> Kotlin Class/File
-- Name : ResultActivity
------------------------------------------------------------------------------------
class ResultActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_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)
// 1등 그림 이름과 그림 파일을 보여준다.
val tvTop = findViewById<View>(R.id.tvTop) as TextView
val ivTop = findViewById<View>(R.id.ivTop) as ImageView
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])
// 9개의 TextView, RatingBar 객체배열
val tv = arrayOfNulls<TextView>(imageName.size)
val rbar = arrayOfNulls<RatingBar>(imageName.size)
// 9개의 TextView, RatingBar ID 배열
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)
// TextView, RatingBar 개체 찾기.
for (i in voteResult!!.indices) {
tv[i] = findViewById<View>(tvID[i]) as TextView
rbar[i] = findViewById<View>(rbarID[i]) as RatingBar
}
// 각 TextVeiw 및 RatingBar에 넘겨 받은 값을 반영.
for (i in voteResult.indices) {
tv[i]?.setText(imageName[i])
rbar[i]?.setRating(voteResult[i].toFloat())
}
val btnReturn = findViewById<View>(R.id.btnReturn) as Button
btnReturn.setOnClickListener { finish() }
}
}
------------------------------------------------------------------------------------
AndroidManifest.xml
------------------------------------------------------------------------------------
추가하기
<activity
android:name=".ResultActivity" >
</activity>
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.IntenRatingBar" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#00897B</item>
</style>
<style name="Theme.IntenRatingBar" parent="Base.Theme.IntenRatingBar" />
</resources>