본문 바로가기

major/Android

[Kotlin] 액티비티 전환하기 - intent, putExtra

1. Intent로 액티비티 전환하기

실행화면

위 캡쳐 화면처럼 MainActivity에서 Button을 눌렀을 때 NewActivity로 넘어가도록 코드를 짰습니다.

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button = findViewById(R.id.button) as Button
    }

    fun onClickButton(view : View){
        var intent = Intent(this, NewActivity::class.java)
        startActivity(intent)
    }
}
<!-- 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:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClickButton"/>

</LinearLayout>
// NewActivity.kt
class NewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)
    }
}
<!-- activity_new.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:orientation="vertical"
    tools:context=".NewActivity">


</LinearLayout>

 

2. putExtra 로 데이터 전달

이번에는 putExtra 함수를 사용해서 NewActivity에 데이터를 전달해보겠습니다.

실행 화면

위 캡쳐 화면처럼 MainActivity에서 Button을 눌렀을 때 EditText의 내용이 NewActivity로 넘어가서 TextView에 보여지도록 짰습니다.

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var textView : TextView
    private lateinit var editText: EditText
    private lateinit var editText2: EditText
    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editText = findViewById(R.id.edittext) as EditText
        editText2 = findViewById(R.id.edittext2) as EditText

        button = findViewById(R.id.button) as Button

    }

    fun onClickButton(view : View){
        var intent = Intent(this, NewActivity::class.java)
        intent.putExtra("name",editText.text.toString())
        intent.putExtra("blog",Integer.parseInt(editText2.text.toString()))
        startActivity(intent)
    }
}
<!-- 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:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hello World!"/>

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClickButton"/>

</LinearLayout>
// NewActivity.kt
class NewActivity : AppCompatActivity() {

    private lateinit var textView1: TextView
    private lateinit var textView2: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)

        textView1 = findViewById(R.id.new_textview1) as TextView
        textView2 = findViewById(R.id.new_textview2) as TextView

        var intent = getIntent()

        textView1.setText(intent.getStringExtra("name"))
        textView2.setText(intent.getIntExtra("blog",0).toString())
    }
}
<!-- activity_new.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:orientation="vertical"
    tools:context=".NewActivity">

    <TextView
        android:id="@+id/new_textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/new_textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

잘못된 내용이 있다면 언제든지 댓글이나 메일로 알려주시면 감사하겠습니다.

이 포스팅이 도움이 되었다면 공감 부탁드립니다.

궁금한 점은 언제든지 댓글 남겨주시면 답변해드리겠습니다:D