1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activityexam">
<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=".SecondActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /><!-- 액티비티 사작점인을 뜻하는 메인 액티비티-->
<category android:name="android.intent.category.LAUNCHER" /><!-- 런처(launcher,휴대폰 홈화면)에서 실행 될수 잇는 액티비티임을 정의-->
</intent-filter>
</activity>
</application>
</manifest>
2. 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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이름"
android:id="@+id/name_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="나이"
android:id="@+id/age_edit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="전송"
android:id="@+id/submit_edit"/>
</LinearLayout>
3. activity_second.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=".SecondActivity">
<!--tools:text="데이터" 프리뷰에서만 보이는 값-->
<TextView
tools:text="데이터"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message_edit_text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/result_button"
android:text="결과전달"/>
</LinearLayout>
4. MainActivity.java
package com.example.activityexam;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final int REQUEST_CODE = 1000;
private EditText mNameEditText;
private EditText mAgeEditText;
//OnCreate() 메서드는 액티비티가 생성될 때 호출되는 콜백 메서드(내가 메서드를 실행하지 않아도 어떤 순간에 스스로 호출됨)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //레이아웃 구성(표시)
//레이아웃에서 입려된 이름과 나이
mNameEditText=findViewById(R.id.name_edit);
mAgeEditText=findViewById(R.id.age_edit);
//버튼 이벤
findViewById(R.id.submit_edit).setOnClickListener(this);
}
@Override
public void onClick(View view) {
//SecondActivity로 전환하는 Intent / 여기서 this는 MainActivity 자신을 가르킴
Intent intent=new Intent(this,SecondActivity.class);
// 이름, 나이 Intent에 추가
intent.putExtra("name",mNameEditText.getText().toString());
intent.putExtra("age",mAgeEditText.getText().toString());
//startActivity(intent);
//Intent의 정보로 토대로 다른 Activity를 시작
startActivityForResult(intent,REQUEST_CODE);
}
//SecondActivity에서 돌려받은 결과 처
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 실패했을 경우 RESULT_CANCEL
if(requestCode == REQUEST_CODE && resultCode==RESULT_OK && data !=null){
//약속된 키값 result로 결과를 받음
String result = data.getStringExtra("result");
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}
}
}
5. SecondActivity.java
package com.example.activityexam;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mMessageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//넘어온 값을 표시
Intent intent=getIntent();
String name=intent.getStringExtra("name");
String age=intent.getStringExtra("age");
mMessageTextView = findViewById(R.id.message_edit_text);
mMessageTextView.setText(age+"살 "+name);
findViewById(R.id.result_button).setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("result",mMessageTextView.getText().toString());
//RESULT_OK, RESULT_CANCEL AppCompatActivity에 상수로 정의
setResult(RESULT_OK,intent);
finish();
}
}
'Android' 카테고리의 다른 글
인텐트 사용방법 (0) | 2020.06.24 |
---|---|
안드로이드 4대 구성요소 (0) | 2020.05.24 |
Gradle Script (0) | 2020.05.24 |
res : 리소스 (0) | 2020.05.24 |
AndroidManifest.xml (0) | 2020.05.23 |