Android Application

Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent) - 양방향 데이터 전달하기

작성자 임베디드코리아 작성일23-10-23 22:51 조회1,400회 댓글0건

첨부파일

Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)
- 양방향 데이터 전달하기
-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edtNum1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#CFFBED"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="80dp">
    </EditText>

    <EditText
        android:id="@+id/edtNum2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#F4F0CC"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="80dp"/>

    <Button
        android:id="@+id/btnNewActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Second Activiy에서 더하기" />
</LinearLayout>

-------------------------------------------------------------------------------------
*****      activity_second.xml **
--- res/layout 에서  ---> New  --> XML --> Layout XML File
--- Layout File Name : activity_second  ,  Root Tag : main
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/reciveData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"/>

    <Button
        android:id="@+id/btnReturn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button"
        android:layout_marginTop="20dp"
        android:text="돌아가기" />
</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        title = "메인 액티비티"

        var btnNewActivity = findViewById<Button>(R.id.btnNewActivity)
        btnNewActivity.setOnClickListener {
            var edtNum1 = findViewById<EditText>(R.id.edtNum1)
            var edtNum2 = findViewById<EditText>(R.id.edtNum2)

            var intent = Intent(applicationContext, SecondActivity::class.java)

            intent.putExtra("Num1", edtNum1.text.toString())
            intent.putExtra("Num2", edtNum2.text.toString())

            //intent.putExtra("Num1", Integer.parseInt(edtNum1.text.toString()))
            //intent.putExtra("Num2", Integer.parseInt(edtNum2.text.toString()))
            startActivityForResult(intent, 0)

            edtNum1.setOnClickListener  {
                edtNum1.setText(null)
            }

            edtNum2.setOnClickListener  {
                edtNum2.setText(null)
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK) {
            var hap = data!!.getIntExtra("Hap", 0)
            Toast.makeText(applicationContext, "합계 :$hap", Toast.LENGTH_SHORT).show()
        }
    }
}

------------------------------------------------------------------------------------
*****    SecondActivity.kt **
-- New --> Kotlin Class/File
-- Name : SecondActivity
------------------------------------------------------------------------------------
class SecondActivity : AppCompatActivity() {
    lateinit var  reciveData1: String
    lateinit var  reciveData2: String

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

        title = "Second 액티비티"

        var btnReturn = findViewById<Button>(R.id.btnReturn)
        var textView = findViewById<TextView>(R.id.reciveData)

        var inIntent = intent
        reciveData1 = inIntent.getStringExtra("Num1").toString()
        reciveData2 = inIntent.getStringExtra("Num2").toString()
        textView.text="    Main Activity로 부터 받은 값은 $reciveData1 와 $reciveData2 입니다." +
                "\n"+"    되돌아가기 버튼을 클릭하면, " +
                "\n"+"    더하기 한 값을 Main Activity로 보냄니다."

        //var hapValue = inIntent.getIntExtra("Num1", 0) + inIntent.getIntExtra("Num2", 0)

        var hapValue = reciveData1.toInt()+reciveData2.toInt()

        btnReturn.setOnClickListener {
            var outIntent = Intent(applicationContext, MainActivity::class.java)
            outIntent.putExtra("Hap", hapValue)
            setResult(Activity.RESULT_OK, outIntent)
            finish()
        }
    }
}

------------------------------------------------------------------------------------
    AndroidManifest.xml
------------------------------------------------------------------------------------
        추가하기
        <activity
            android:name=".SubActivity" >
        </activity>

------------------------------------------------------------------------------------
    botton.xml
-- res/drawable
-- New --> Drawable Resource File
-- File Name : button  , Root Element : shape
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#8EC7F4" />
    <stroke android:color="#ff0000ff" android:width="4dp" />
    <padding android:left="7dp" android:top="7dp"
        android:right="7dp" android:bottom="7dp" />
    <corners
        android:radius="30dp"
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
</shape>

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ExplicitIntentDuplexData" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#00897B</item>
    </style>

    <style name="Theme.ExplicitIntentDuplexData" parent="Base.Theme.ExplicitIntentDuplexData" />
</resources>