액티비티 간의 연결고리 역할을 함.
#. 첫번째 Activity
public class ActivityTest extends Activity {
protected static final int GET_CODE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* AndroidManifest.xml의 application에서 액티비티를 추가한후 intent 필터 설정해준다
* <intent-filter>
* <action android:name="android.intent.action.MAIN" />
* <category android:name="android.intent.category.LAUNCHER" />
* </intent-filter>
* 자세한 설명은 http://www.androidside.com/B46/137 참조.. */
setContentView(R.layout.actvity1);
Button button =(Button)findViewById(R.id.startButton);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
EditText edit1 =(EditText)findViewById(R.id.text1);
EditText edit2 =(EditText)findViewById(R.id.text2);
// intent(넘겨줄 activity, 받을 activity)
// intent 객체만 생성해주는 것일뿐 동작은 startActivity(Intent intent)로 동작..
Intent intent = new Intent(ActivityTest.this, ActivityTest1.class);
/* 받을 Activity에서 TEXT1이라는 변수명으로 사용
* putExtra(name, value) ==>(data set 할 때)
* get<Type>Extra(name)==>(data get할 때) ex)getStringExtra */
intent.putExtra("TEXT1", edit1.getText().toString());
intent.putExtra("TEXT2", edit2.getText().toString());
// activity가 끝날때 결과물을 받음 인자는 (intent, 호출구분코드)
// 아래 onActivityResult와 쌍으로 동작한다..
startActivityForResult(intent, GET_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 설명하자면 현재 activity에서 textview를 가져온후 호출구분코드가 조건에 맞고
// 다른 activity에서 넘겨준 호출구분코드가 조건에 맞으면 현재 액티비티에 버튼이 입력된값 출력함..
TextView view =(TextView)findViewById(R.id.text);
if (requestCode == GET_CODE) {
if (resultCode == RESULT_OK)
view.setText(data.getAction());
}
}
}
#. 두번째 액티비티
public class ActivityTest1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button button = (Button)findViewById(R.id.button1);
Intent intent = getIntent();
String t1 = intent.getStringExtra("TEXT1"); // 액티비티1의 TEXT1로 지정한 내용 가져옴
String t2 = intent.getStringExtra("TEXT2");
button.setText(t1);
button.setOnClickListener(mCorkyListener);
button = (Button)findViewById(R.id.button2);
button.setText(t2);
button.setOnClickListener(mVioletListener);
}
private OnClickListener mCorkyListener = new OnClickListener()
{
public void onClick(View v)
{
Button button1 = (Button)findViewById(R.id.button1);
// To send a result, simply call setResult() before your
// activity is finished.
// RESULT_OK 라는 호출구분코드로 넘겨줘서 구분한다.
setResult(RESULT_OK, (new Intent()).setAction(button1.getText().toString()));
finish();
}
};
private OnClickListener mVioletListener = new OnClickListener()
{
public void onClick(View v)
{
// To send a result, simply call setResult() before your
// activity is finished.
Button button2 = (Button)findViewById(R.id.button1);
setResult(RESULT_OK, (new Intent()).setAction(button2.getText().toString()));
finish();
}
};
}
#. layoutl.xml 파일
-. 첫번재 액티비티 layout xml (Activity1.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"
>
<EditText
android:id="@+id/text1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
/>
<EditText
android:id="@+id/text2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
/>
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Activity" />
</LinearLayout>
-. 두번재 액티비티 layout xml (Activity2.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"
>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
/>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
/>
</LinearLayout> 참조 : http://www.androidside.com/B46/66
문제시 포스팅 삭제할께요~
댓글 없음:
댓글 쓰기