간단하게 액티비티를 이동시키고 데이터를 주고 받아보자.
모든 소스는 잘 돌아가는 걸 P&D했으니 긁어다 실행하다보면 어디가 중요한지 알 수 있을 것이다.
소스 외에 중요 부분만 알고 싶다면 제일 아래로 드래그
1. 보내고 받는 액티비티의 레이아웃
1) send_activity.xml
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
tools:context=".BasicFormActivity" />
<EditText
android:id="@+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"/>
<RadioGroup
android:id="@+id/radiogroup_sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="남"/>
<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sms"/>
<CheckBox
android:id="@+id/checkbox_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/interest"/>
<Spinner
android:id="@+id/spinner_interest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/birthday"/>
<EditText
android:id="@+id/edit_birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전송" />
</LinearLayout>
</LinearLayout>
2) recv_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title_receive" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_name" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/sex" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_sex" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/sms" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_sms" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/interest" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_interest" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="60sp"
android:layout_height="wrap_content"
android:text="@string/birthday" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_birthday" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/button_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이전" />
</LinearLayout>
</LinearLayout>
2. 그에 관한 소스
1) SendActivity.java
package com.activitytrans; import java.util.Calendar; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.DatePicker; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.TextView; public class SendActivity extends Activity implements OnClickListener { private TextView mDateDisplay; private int mYear; private int mMonth; private int mDay; static final int DATE_DIALOG_ID = 0; Spinner spinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); ArrayAdapteradapter = ArrayAdapter.createFromResource( this, R.array.interest_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner = (Spinner) findViewById(R.id.spinner_interest); spinner.setAdapter(adapter); mDateDisplay = (TextView) findViewById(R.id.edit_birthday); mDateDisplay.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); updateDisplay(); Button btn = (Button) findViewById(R.id.button_send); btn.setOnClickListener(this); } protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } }; private void updateDisplay() { mDateDisplay.setText(new StringBuilder().append(mYear).append("-") .append(mMonth + 1).append("-").append(mDay).append("")); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_send, menu); return true; } public void onClick(View v) { EditText et_name = (EditText) findViewById(R.id.edit_name); String str_name = et_name.getText().toString(); RadioGroup rg_sex = (RadioGroup) findViewById(R.id.radiogroup_sex); RadioButton rb_male = (RadioButton) findViewById(R.id.radio_male); RadioButton rb_female = (RadioButton) findViewById(R.id.radio_female); String str_sex = ""; if (rg_sex.getCheckedRadioButtonId() == R.id.radio_male) { str_sex = rb_male.getText().toString(); } if (rg_sex.getCheckedRadioButtonId() == R.id.radio_female) { str_sex = rb_female.getText().toString(); } CheckBox chk_sms = (CheckBox) findViewById(R.id.checkbox_sms); String str_sms = ""; if (chk_sms.isChecked()) { str_sms = (String) chk_sms.getText(); } String str_interest = spinner.getSelectedItem().toString(); EditText et_birthday = (EditText) findViewById(R.id.edit_birthday); String str_birthday = et_birthday.getText().toString(); // // 중요 부분! // Intent it = new Intent(this, ReceiveActivity.class); it.putExtra("it_name", str_name); it.putExtra("it_sex", str_sex); it.putExtra("it_sms", str_sms); it.putExtra("it_interest", str_interest); it.putExtra("it_birthday", str_birthday); startActivity(it); finish(); // // 중요 부분! // /* * AlertDialog.Builder alert = new AlertDialog.Builder(this); * alert.setTitle("알림창"); * alert.setMessage("성명: "+str_name+"\n성별: "+str_sex * +"\n수신여부 : "+str_sms+ * "\n관심분야 : "+spn_interest+"\n생일 : "+str_birthday); * alert.setIcon(R.drawable.ic_launcher); alert.setPositiveButton("확인", * null); alert.show(); */ } }
2) RecvActivity.java
package com.activitytrans; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ReceiveActivity extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receive); Intent it = getIntent(); String str_name = it.getStringExtra("it_name"); String str_sex = it.getStringExtra("it_sex"); String str_sms = it.getStringExtra("it_sms"); String str_interest = it.getStringExtra("it_interest"); String str_birthday = it.getStringExtra("it_birthday"); TextView tv_name = (TextView) findViewById(R.id.tv_name); tv_name.setText(str_name); TextView tv_sex = (TextView) findViewById(R.id.tv_sex); tv_sex.setText(str_sex); TextView tv_sms = (TextView) findViewById(R.id.tv_sms); tv_sms.setText(str_sms); TextView tv_interest = (TextView) findViewById(R.id.tv_interest); tv_interest.setText(str_interest); TextView tv_birthday = (TextView) findViewById(R.id.tv_birthday); tv_birthday.setText(str_birthday); Button btn = (Button) findViewById(R.id.button_prev); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_receive, menu); return true; } // // 중요 부분! // public void onClick(View v) { Intent it = new Intent(this, SendActivity.class); startActivity(it); finish(); } // // 중요 부분! // }
3. 주요 부분과 설명
// // 중요 부분! // public void onClick(View v) { Intent it = new Intent(this, SendActivity.class); startActivity(it); finish(); } // // 중요 부분! //
이 부분을 보면 Intent 라는 것이 있는데 이 친구로 액티비티를 왔다 갔다 할 수 있는 것이다.
생성자 내용은 (패키지장소, 자바 클래스 파일)을 적어주면 되고,
시작은 startActivity()를 통해서, 그리고 액티비티의 소멸은 finish()를 통해서 하게 된다.
보통 다른 액티비티로 넘어간 뒤에는 자기자신을 죽이게 된다.
인자값을 넘기려면, .putExtra("변수명", 값); 을 해주면 된다.
예로 들자면, SendActivity에 값을 전달하고 싶으면
it.putExtra("it_name", "데르벨준"); 이라고 적어주면 된다.
전달된 값을 실행시킨 액티비티에서 받아오는 방법은
it.getStringExtra("it_name"); 이라 해주면 된다.
그리고 전달되는 값은 String으로 넘기고 받게 된다.
'Android' 카테고리의 다른 글
JSP에서 DB 연결 방법 (0) | 2013.01.14 |
---|---|
안드로이드의 데이터베이스 이용 / 웹 서버 / JSON, XML 파서의 이해 (2) | 2013.01.14 |
화면 고정하기 (0) | 2013.01.14 |
비트맵Bitmap을 이용한 썸네일/축소와 회전 (0) | 2013.01.14 |
에디트텍스트 EditText 입력글자 제한하기 (0) | 2013.01.14 |
WRITTEN BY