0
1

More than 3 years have passed since last update.

【Kotlin 初学者】画面遷移時で値を渡す方法

Last updated at Posted at 2021-03-16

画面遷移時に値を渡す方法

EditTextなどで入力した値を別画面に渡したいなど頻繁に使われる実装になります
本当はViewBindingでid取得したかったんですが、それはまた別記事に書きますので、まずはfindViewByIdでxmlのidを取得してください🙇‍♂️

イメージサンプル

実際のコードになります

画面遷移先をNextActivityにします
EditTextで値を入力し、Intentで画面遷移時にNextActivityで値を受け取り、
TextViewに代入してます

MainActivity.kt
class MainActivity : AppCompatActivity() {

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

        var editText = findViewById<EditText>(R.id.edit_text)
        var inputText = editText.editableText

        val nextButton = findViewById<Button>(R.id.next_button)

        nextButton.setOnClickListener {
            if(editText.text != null){
                val intent = Intent(this, NextActivity::class.java)
                intent.putExtra("INPUT_TEXT",inputText.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/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="ここに値を入力する"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="クリック"
        app:layout_constraintTop_toBottomOf="@+id/edit_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
NextActivity.kt
class NextActivity : AppCompatActivity(){

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

        var getText = findViewById<TextView>(R.id.get_text)
        val intentText = intent.getStringExtra("INPUT_TEXT")

        getText.text = intentText

        val returnButton = findViewById<Button>(R.id.return_button)
        returnButton.setOnClickListener {
            finish()
        }
    }
}

AndroidManifestなどはQiita上に記載してないので、
困ったらGithubから確認してください

終わりに

Githubにもサンプル乗せているのでよかったらどうぞ!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1