메뉴 건너뛰기

조회 수 7676 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

* 현재 액티비티(DtatTransforActivity의 TextView의 내용을 datatransfor에 전달해 봅니다.

* 그리고 datatransfor 엑티비티의 EditText에 입력한 내용을 되돌려줍시다.

1.jpg

3.jpg







































3.jpg 4.jpg



1. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/text"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Sample"
    />
<Button
    android:id="@+id/btnedit"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="mOnClick"
    android:text="Edit"
    />
</LinearLayout>






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >
<EditText
android:id="@+id/stredit"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  />
<Button
    android:id="@+id/btnok"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="mOnClick"
    android:text="OK"/>
<Button
android:id="@+id/btncancel"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="mOnClick"
    android:text="Cancel" />
</LinearLayout>



2. java code

* 중요 메소드

1. startActivityForResult : 이전 포스팅에서는 화면만 이동하는 Activity클래스의 메서드인 startActivity()를 사용했다면 해당 메소드는 ForResult 즉 액티비티이동 후 다시 되돌아올 때 결과를 가지고 올수 있는 메서드입니다.


2. Intent.putExtra("키","데이터") : 액티비티를 이동할 때 전달하고자 하는 데이터를 해당 메소드를 통해 Intent에 담아갑니다.


4. Activity.getIntent : 새로 이동한 액티비티에서는 getIntent()를 통해 전달 받은 Intent객체의 정보를 가져올 수 있습니다. 


5. onActivityResult() : startAcitivityForResult()를 이용하여 액티비티를 이동할 경우 새로 호출한 액티비티가 종료될 때

콜백메서드로 onActivityResult()를 호출하게 되는데 해당 메서드의 매개변수에는 사라진 액티비티의 Intent 정보가 넘어옵니다.


package com.example.datatransforactivity;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void mOnClick(View v){
        
        Intent intent = new Intent(this,TransferActivity.class);
        TextView txt = (TextView)findViewById(R.id.text);
        //intent에 data라는 키로 데이털르 저장해서 다음 Activity로 이동
        intent.putExtra("data",txt.getText().toString());
        
        
        //ShareData.getInstance().name = txt.getText().toString();
        // 인텐트를 화면에 출력하고 인텐트가 화면에서 제거될때 콜백메소드를 호출하도록 설정
        startActivityForResult(intent,1); 
        
    }
    //콜백메서드 정의
    //현재화면위에 출력된 액티비티가 화면에서 제거될때 호출되는 메소드
    @Override
    protected void onActivityResult(int requestCode,int reusltCode, Intent data){
        String msg = data.getStringExtra("data");
        TextView txt = (TextView)findViewById(R.id.text);
        txt.setText(msg);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



package com.example.datatransforactivity;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
 
public class TransferActivity extends Activity {
 
    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.actedit);
        EditText edit = (EditText)findViewById(R.id.stredit);
        
        
        //자기 인텐트를 부릅니다
        Intent intent = getIntent();
        //data라는 키로 저장된 데이터를 문자열로 받아서 저장
        String data = intent.getStringExtra("data");
        edit.setText(data);
        
    }
    
    public void mOnClick(View v){
        
        //호출한 엑티비티에게 데이터를 전달하기 위해서 intent를 생성
        Intent intent = new Intent();
        EditText edit = (EditText)findViewById(R.id.stredit);
        intent.putExtra("data", edit.getText().toString());
        setResult(1,intent);//정수는 아무거나 주시면됩니다
        
        
        
        finish();
    }
}








List of Articles
번호 제목 날짜 조회 수
157 노티피케이션(Notification) 사용법 / Notification.Builder , NotificationManager file 2016.06.10 13470
156 어댑터 뷰(Adapter View) & 어댑터(Adapter) (1) file 2016.06.08 7852
» Activity Data Transfor/ 액티비티 이동간에 데이터 전송하기 file 2016.06.07 7676
154 Activity Switching / 안드로이드 액티비티 전환 / 화면 전환 file 2016.06.07 8311
153 알아놓으면 좋은 내용정리 2016.06.07 7458
152 간단한 mp3 플레이어 만들기 , 음악넣기 , 노래재생 file 2016.06.07 14622
151 암시적 인텐트를 사용한 인터넷열기, 전화걸기, 문자보내기 [Intent (인텐트)] file 2016.06.07 7736
150 Intent (인텐트) 2016.06.07 7626
149 Android 와 JSP 간 파라미터 암복호화 (3) file 2016.05.26 8088
148 Android 와 JSP 간 파라미터 암복호화 (2) 2016.05.26 7735
147 Android 와 JSP 간 파라미터 암복호화 (1) file 2016.05.26 7474
146 카카오톡 대화내용 가져오기(sqlite3, chat_logs) file 2016.05.26 15107
145 카카오톡 분석하기 (2) - 카카오톡 암호화 함수 찾기 file 2016.05.26 9598
144 카카오톡 분석하기 (1) - sqlite 파해치기 file 2016.05.26 10062
143 안드로이드용 채팅프로그램 클라이언트(java), 서버(c#) 소스 file 2016.05.19 11720
142 네트워크 연결 상태 및 3G/WIFI 연결상태 체크하기 2016.03.18 7131
141 Android Push GCM 프로젝트 앱 적용 하기(2) file 2016.03.18 8953
140 안드로이드] 페이스북 같은 슬라이드 메뉴 만들기 file 2015.12.15 12535
139 Android TIP] strings.xml 에서 특수문자 사용하기 2015.12.15 6629
138 Android] Fragment 내부의adapter에서 startActivity 하기 2015.12.15 6487
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved