- Intent 활용 예제 ExIntentActivity.java 파일
- Activity 하나를 열어 입력한 값을 찍는다.
- 똑같은 Activity를 반복해 호출함.
- Log.i()는 정보 information를 LogCat에 찍는다.
- Log.i()를 사용하려면 import android.util.*; 또는 import android.util.Log가 필요하다.
package soo.intent.ex.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.util.*;
public class ExIntentActivity extends Activity {
//int iLogFlow = 0;
EditText et1, et2;
Button b;
Intent i1, i2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)findViewById(R.id.EditText01);
et1.requestFocus();
et2 = (EditText)findViewById(R.id.EditText02);
b = (Button)findViewById(R.id.Button01);
Log.i("ExIntentActivity", " b4 onClick"); // Log찍기
i2 = new Intent(this, ExIntentActivity.class);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("ExIntentActivity", "onClick()"); // Log찍기
i2.putExtra("key", et1.getText().toString());
ExIntentActivity.this.startActivity(i2);
Log.i("ExIntentActivity", "onClick() a4 startActivity"); // Log찍기
et1.setText("입력한 값: "+ et1.getText().toString() + "<====이 줄은 순식간에 사라짐");
ExIntentActivity.this.finish();//onDestroy()호출
}
});
i1 = getIntent();
getData();
}
void getData(){
Log.i("ExIntentActivity", "getData()"); // Log찍기
String value = i1.getStringExtra("key");
if(value != null) et2.setText(value);
}
}
=====================
아래는 main.xml파일
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20px"
android:text="매인 Main Activity"
android:gravity="center"
android:textSize="40px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전송"/>
</LinearLayout>
<EditText
android:id="@+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:editable="false"
android:hint="여기에 전송결과 뿌려짐!!"/>
</LinearLayout>