반응형
Notice
Recent Posts
Recent Comments
Link
불로구
안드로이드 스튜디오 - Intent (1) 본문
반응형
Intent란?
- 의도, 요구, 의사 전달, 요청
- 요청 관계
-> 액티비티와 액티비티
-> 안드로이드 시스템과 myApp
-> 다른 앱과 내 앱 ( 상호합의 필요)
- 요청의 전달
-> 전달만 하는 요청
-> 리턴을 받는 요청
- 버튼을 클릭하면 editText값에 따라 전달받은 액티비티의 textView에 값 변경
MainActivity
package com.example.intent
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
private val intent_button : Button by lazy{ findViewById(R.id.intent_button) }
private val input_number : EditText by lazy { findViewById(R.id.input_number) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
intent_button.setOnClickListener{
var intent = Intent(this,IntentActivity::class.java)
intent.putExtra("data",
input_number.text.toString()
)
startActivity(intent)
}
}
}
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/input_number"
android:hint="숫자입력"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/intent_button"/>
<Button
android:id="@+id/intent_button"
android:text="인텐트 넘기기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
IntentActivity
package com.example.intent
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class IntentActivity : AppCompatActivity() {
private val view_intent : TextView by lazy { findViewById(R.id.view_intent) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_intent)
var num = intent.getStringExtra("data")
when(num){
"1" -> view_intent.setText("1이 들어옴")
"2" -> view_intent.setText("2가 들어옴")
else -> view_intent.setText("아무것도 안들어옴")
}
}
}
activity_intent.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/view_intent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
반응형
'프로그래밍 > 코틀린' 카테고리의 다른 글
코틀린(Kotlin) - lateinit (0) | 2021.04.12 |
---|---|
코틀린(Kotlin) - NullSafety (0) | 2021.04.12 |
[코틀린 기초] 15. 전역변수, 지역변수 (0) | 2021.03.19 |
[Kotlin] - 계산기 코드 (0) | 2021.03.10 |
[코틀린 기초] 14. 클래스(Class) <2> (0) | 2021.03.07 |
Comments