이 예제는 Kotlin을 사용하여 Android의 Main Activity로 데이터를 다시 보내는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginBottom="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <TextView android:id="@+id/textViewNumbers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Numbers: " android:textColor="@android:color/black" android:textSize="24sp" android:textStyle="bold" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="add" /> <Button android:id="@+id/buttonSubtract" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="subtract" /> </LinearLayout>
3단계 − src/MainActivity.kt에 다음 코드 추가
import android.app.Activity import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var textViewResult: TextView private lateinit var editTextNumber1: EditText private lateinit var editTextNumber2: EditText private lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" textViewResult = findViewById(R.id.textViewResult) editTextNumber1 = findViewById(R.id.editTextNumber1) editTextNumber2 = findViewById(R.id.editTextNumber2) button = findViewById(R.id.btnOpenActivity2) button.setOnClickListener { if ((editTextNumber1.text.toString() == "" || editTextNumber2.text.toString() == "")) { Toast.makeText(this@MainActivity, "Please insert numbers", Toast.LENGTH_SHORT).show() } else { val number1 = Integer.parseInt(editTextNumber1.text.toString()) val number2 = Integer.parseInt(editTextNumber2.text.toString()) val intent = Intent(this@MainActivity, SecondActivity::class.java) intent.putExtra("number1", number1) intent.putExtra("number2", number2) startActivityForResult(intent, 1) } } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == 1) { if (resultCode == Activity.RESULT_OK) { val result = data!!.getIntExtra("result", 0) textViewResult.text = "" + result } if (resultCode == Activity.RESULT_CANCELED) { textViewResult.text = "Nothing selected" } } } }
4단계 − 새 활동을 만들고 다음 코드를 추가하십시오. −
activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginBottom="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <TextView android:id="@+id/textViewNumbers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Numbers: " android:textColor="@android:color/black" android:textSize="24sp" android:textStyle="bold" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="add" /> <Button android:id="@+id/buttonSubtract" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="subtract" /> </LinearLayout>
SecondActivity.kt
import android.app.Activity import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class SecondActivity : AppCompatActivity() { lateinit var textViewNumber: TextView lateinit var buttonAdd: Button lateinit var buttonSubtract: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) val intent = intent val number1 = intent.getIntExtra("number1", 0) val number2 = intent.getIntExtra("number2", 0) textViewNumber = findViewById(R.id.textViewNumbers) textViewNumber.text = "Numbers: $number1, $number2" buttonAdd = findViewById(R.id.buttonAdd) buttonSubtract = findViewById(R.id.buttonSubtract) buttonAdd.setOnClickListener { val result = number1 + number2 val resultIntent = Intent() resultIntent.putExtra("result", result) setResult(Activity.RESULT_OK, resultIntent) finish() } buttonSubtract.setOnClickListener { val result = number1 - number2 val resultIntent = Intent() resultIntent.putExtra("result", result) setResult(Activity.RESULT_OK, resultIntent) finish() } } }
5단계 − androidManifest.xml에 다음 코드 추가
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행 아이콘을 클릭하세요. 툴바에서 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오.