Android Application
Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)-데이터 전달하기
작성자 임베디드코리아
작성일23-10-23 22:50
조회1,403회
댓글0건
Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)
- Sub Activity로 데이터 전달하기
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="352dp"
android:layout_marginEnd="20dp"
android:hint="텍스트를 입력하세요"
app:layout_constraintBottom_toTopOf="@+id/btnGoSubActivity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnGoSubActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="236dp"
android:text="Go SubActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
-------------------------------------------------------------------------------------
***** activity_sub.xml **
--- res/layout 에서 ---> New --> Layout Resource File
--- File : activity_sub , Source set : main
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SubActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="352dp"
android:layout_marginEnd="20dp"
android:text="수신된 문자열"
android:textSize="20sp"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/btnClose"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="236dp"
android:text="Close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var btnGoSubActivity: Button
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnGoSubActivity = findViewById(R.id.btnGoSubActivity)
editText = findViewById(R.id.editText)
btnGoSubActivity.setOnClickListener {
val text = editText.text.toString()
val intent = Intent(this, SubActivity::class.java)
intent.putExtra("Data", text)
startActivity(intent)
}
}
}
------------------------------------------------------------------------------------
***** SubActivity.kt **
-- New --> Kotlin Class/File
-- Name : SubActivity
------------------------------------------------------------------------------------
class SubActivity: AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var btnClose: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
textView = findViewById(R.id.textView)
btnClose = findViewById(R.id.btnClose)
textView.text = intent.getStringExtra("Data")
btnClose.setOnClickListener {
finish()
}
}
}
------------------------------------------------------------------------------------
AndroidManifest.xml
------------------------------------------------------------------------------------
추가하기
<activity
android:name=".SubActivity" >
</activity>