결과화면

 

 

 

 

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>

 

--------------------------------------------------------------------------------------------------------

 

IntentCallee.java

 

 package com.example.hellow;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class IntentCallee extends Activity{
 TextView txtName, txtBitrh;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.callee);
  
  txtName = (TextView)findViewById(R.id.txt_name);
  txtBitrh = (TextView)findViewById(R.id.txt_birth);
  
  Intent intent = getIntent();
  String name = intent.getExtras().get("name").toString();
  String birth = intent.getExtras().get("birth").toString();
  
  txtName.setText(name);
  txtBitrh.setText(birth);
  
  Button result = (Button)findViewById(R.id.ok);
  result.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    Calendar now = Calendar.getInstance();
    int nowYear = now.get(Calendar.YEAR) + 1;
    int age = nowYear - Integer.parseInt(txtBitrh.getText().toString());
    Intent intent = new Intent(IntentCallee.this, IntentCaller.class);
    intent.putExtra("name", txtName.getText().toString());
    intent.putExtra("age", String.valueOf(age));
    setResult(Activity.RESULT_OK, intent);
    finish();
   }
  });
 }
}

 

--------------------------------------------------------------------------------------------------

 

callee.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="이름 : " />

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </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="태어난 년도 : " />

        <TextView
            android:id="@+id/txt_birth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="결과 보내기" />

</LinearLayout>

 

 

'2020년도 이전 > [WebSig] Android' 카테고리의 다른 글

Button Demo  (0) 2013.08.13
EditText Demo  (0) 2013.08.13
TextView Demo  (0) 2013.08.13
암시적 Intent 사용 예제  (0) 2013.08.13
BroadcastReceiver 예제...  (0) 2013.08.12

+ Recent posts