- 메인 화면에서 버튼 누르면 값을 전달.

- 받은 화면에서 값을 수정하고 다시 전달.

 

- 서울시 종로구 로 넘김.                              - 서울시 종로구 25로 수정                              - 수정된 데이터로 돌아온 화면

    

 

(1) activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="서울시 종로구" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txt1"
        android:text="주소수정하기" />

</RelativeLayout>

 

 

(2) sub.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/edtAddr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>
    <Button
        android:id="@+id/subBtnOk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="수정" />
    <Button
        android:id="@+id/subBtnCancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="취소" />

</LinearLayout>

 

 

(3) AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test19_intent2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test19_intent2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- 액티비티 등록 -->
        <activity
            android:name="com.example.test19_intent2.SubActivity"
            android:label="넘어온 화면"
            ></activity>

    </application>
</manifest>

 

 

(4) MainActivity.java

package com.example.test19_intent2;

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

public class MainActivity extends Activity {
    Button btn;
    TextView txt1;
    final static int CODE=1;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        txt1 = (TextView)findViewById(R.id.txt1);
        btn = (Button)findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                //인텐트에 보낼 정보 담기
                intent.putExtra("addr", txt1.getText().toString());
                //호출한 액티비티에서 결과값을 받아오는 경우
                startActivityForResult(intent,CODE);

            }
        });
    }

 

    //서브액티비티가 값을 보내오면 자동호출되는 메소드
    //requestCode에 메인 액티비티에서 보낸 CODE가 담겨온다.
    @Override
    //onActivityResult( 메인액티비티에서 보낸 코드,서브액티비티에서 보내온 리절트코드,서브액티비티에서 데이터를 담아 보낸 인텐트 )
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
        case 1:
            if(resultCode==RESULT_OK){//서브Activity에서 보내온 resultCode와 비교
                //서브액티비티에서 인텐트에 담아온 정보 꺼내기
                String edtAddr=data.getStringExtra("edtAddr");
                //텍스트뷰에 수정된 문자열 넣기
                txt1.setText(edtAddr);
            }else{
               
            }
            break;
        }
    }

 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

 

(5) SubActivity.java

package com.example.test19_intent2;

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

public class SubActivity extends Activity{
    Button subBtnOk;
    Button subBtnCancel;
    EditText edtAddr;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);
       
        subBtnOk=(Button)findViewById(R.id.subBtnOk);
        subBtnCancel=(Button)findViewById(R.id.subBtnCancel);
        edtAddr=(EditText)findViewById(R.id.edtAddr);

 

        //메인 액티비티에서 인텐트에 담아온 값 꺼내오기..
        //나를 호출한 인텐트 얻어오기
        Intent intent=getIntent();
        
        //인텐트에 담겨있는 값 꺼내오기
        String addr = intent.getStringExtra("addr");
        
        //에디트텍스트에 문자열 넣기
        edtAddr.setText(addr);
       
        subBtnOk.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {
                //입력된 주소 얻어오기
                String data = edtAddr.getText().toString();
                //수정된 주소 정보를 담기위한 인텐트 생성
                Intent intent=new Intent();
                //수정된 정보 담기
                intent.putExtra("edtAddr", data);
                //result code와 (성공의 의미) 수정데이터를 담은 인텐트 설정
                setResult(RESULT_OK, intent);
                //Activity 종료(메인액티비티로 이동)
                finish();
            }
        });
        subBtnCancel.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {
                //result코드 담기
                setResult(RESULT_CANCELED);
                //Activity 종료하기(자신을 호출한 액티비티로 가기)
                finish();               
            }
        });
    }
}

 

 

'Mobile > Android' 카테고리의 다른 글

#21 ( Intent Action : 외부 프로그램과 연결 )  (0) 2013.02.02
#20 ( 다른 Activity 로 text와 이미지 넘기기 )  (0) 2013.02.02
#18 (Intent 1: 화면 이동)  (0) 2013.02.02
#17 (menu)  (0) 2013.02.02
#16 ( alert )  (0) 2013.02.02

+ Recent posts