IntentCaller.java
package com.example.hellow;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;
public class IntentCaller extends Activity{ public static final int CALLER_REQUEST = 100;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.caller); Button send = (Button)findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText uname = (EditText)findViewById(R.id.userName); EditText byear = (EditText)findViewById(R.id.birthYear); //명시적 인텐트 생성 Intent intent = new Intent(IntentCaller.this, IntentCallee.class); intent.putExtra("name", uname.getText()); intent.putExtra("birth", byear.getText()); startActivityForResult(intent, CALLER_REQUEST); } }); }//onCreate() end
//IntentCallee.class에서 데이터를 받고나서 받은 데이터를 이용하는 메서드 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(CALLER_REQUEST == requestCode){ if(resultCode==Activity.RESULT_OK){ String name = data.getExtras().getString("name").toString(); String age = data.getExtras().get("age").toString(); TextView result = (TextView)findViewById(R.id.txtAge); result.setText(name + "의 나이는 " + age + "입니다."); } } } }
--------------------------------------------------------------------------------------------------
caller.xml
<?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" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="이름 : " />
<EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" >
<requestFocus /> </EditText> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="태어난 년도 : " />
<EditText android:id="@+id/birthYear" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="1989" android:inputType="number" /> </LinearLayout>
<Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="나이 계산하기" />
<TextView android:id="@+id/txtAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:textColor="#0000ff" android:textSize="15sp"/>
</LinearLayout> |