티스토리 뷰
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="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnMoveToSub"
android:layout_width="400dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Move To SubActivity"
android:textSize="30dp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:background="#FF7F00"/>
</LinearLayout>
acitvity_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SubActivity">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_centerInParent="true"
tools:text="Name"/>
<TextView
android:id="@+id/tvNickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvName"
android:layout_centerInParent="true"
tools:text="Nickname"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvNickname"
android:layout_centerInParent="true"
tools:text="Age"/>
<TextView
android:id="@+id/tvFavoriteMovies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvAge"
android:layout_centerInParent="true"
android:singleLine="true"
android:ellipsize="end"
tools:text="FavoriteMovies" />
<TextView
android:id="@+id/tvPrivacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvFavoriteMovies"
android:layout_centerInParent="true"
tools:text="Privacy" />
<Button
android:id="@+id/btnMoveToMain"
android:layout_width="400dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Move To MainActivity"
android:textSize="30dp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:background="#FF7F00"
android:layout_marginTop="100dp"
android:layout_below="@+id/tvPrivacy"
android:layout_centerInParent="true" />
</RelativeLayout>
Privacy.kt
package com.jwsoft.kotlinproject
import java.io.Serializable
data class Privacy (
val height: Int,
val weight: Int
): Serializable
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val favoriteMovies = arrayOf("The Avengers", "Good Will Hunting", "Notting Hill")
val privacy = Privacy(184, 84)
btnMoveToSub.setOnClickListener {
val intent: Intent = Intent(this, SubActivity::class.java)
intent.putExtra("Name", "James Kim")
intent.putExtra("Nickname", "혀가 길지 않은 개발자")
intent.putExtra("Age", 30)
intent.putExtra("Favorite Movies", favoriteMovies)
intent.putExtra("Privacy", privacy)
startActivity(intent)
}
}
}
SubActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_sub.*
class SubActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
val intent = intent
tvName.text = intent.extras?.getString("Name")
tvNickname.text = intent.extras?.getString("Nickname")
tvAge.text = intent.extras?.getInt("Age").toString()
val movies = intent.extras?.getStringArray("Favorite Movies")
var strMovies = ""
if (movies != null) {
for (movie in movies) {
strMovies += "$movie, "
}
}
tvFavoriteMovies.text = strMovies
val privacy: Privacy = intent.extras?.getSerializable("Privacy") as Privacy
tvPrivacy.text = "${privacy.height} / ${privacy.weight}"
btnMoveToMain.setOnClickListener {
val intent: Intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Room (0) | 2020.07.21 |
---|---|
[Kotlin] SQLite + Singleton Pattern (0) | 2020.07.18 |
[Kotlin] CardView (0) | 2020.07.11 |
[Kotlin] SQLite (0) | 2020.07.11 |
[Kotlin] ViewModel (0) | 2020.07.10 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- handler
- 혀가 길지 않은 개발자
- 안드로이드 #코틀린 #Android #Kotlin
- 자바
- XML
- Vue.js #Vue.js + javascript
- 안드로이드
- Kotlin
- Design Pattern
- MVVM
- Android
- 코틀린
- ViewPager2
- CoordinatorLayout
- Livedata
- Intent
- java
- DataBinding
- activity
- James Kim
- TabLayout
- JSONArray
- View
- JSONObject
- recyclerview
- ViewModel
- fragment
- Architecture Pattern
- ArrayList
- coroutine
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함