기술참고자료/Android | 2011. 5. 6. 13:51
package com.test.vo;import android.os.Parcel;
import android.os.Parcelable;/**
* @author sungsik81
*
*/
public class Student implements Parcelable {
public String name;
public int age;
public String birthday;
public Student() {
}public Student(Parcel in) {
readFromParcel(in);
}
public Student(String name, int age, String birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}/* (non-Javadoc)
* @see android.os.Parcelable#describeContents()
*
* Parcel 하려는 오브젝트의 종류를 정의한다.
* 어떤 특별한 객체를 포함하고 있는지에 대한 설명을 리턴값으로 표현 하는 것이라고 보면된다.
* bit mask 형식의 integer를 리턴 하며,값을 체크 할 때 bit mask 체크를 해서 어떤 것들이 들어 있는지 알 수 있다.
*/
@Override
public int describeContents() {
return 0;
}
/* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
*
* Parcel에 데이터를 쓰는 부분.
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(birthday);
}private void readFromParcel(Parcel in) {
name = in.readString();
age = in.readInt();
birthday = in.readString();
}
/**
* @author sungsik81
*
* Parcelable.Creator<T> 클래스는 createFromParcel() 과 newArray() 메소스가 필요하다.
* Parcel로 부터 값을 읽어 오기 위해서는 Parcelable.Creator Interface 가 필요하다.
*/
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator() {//writeToParcel() 메소드에서 썼던 순서대로 읽어 오는 것입니다.
@Override
public Object createFromParcel(Parcel source) {
return new Student(source);
}@Override
public Object[] newArray(int size) {
return new Student[size];
}
};/*
* 이하, Getter/Setter
*/
public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public String getBirthday() {
return birthday;
}public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
MainActivity.java
package com.test;
import com.test.vo.Student;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends Activity {
Button submitButton;
EditText myTextView;
EditText myAge;
EditText myBirthday;
String myText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (EditText) findViewById(R.id.myName);
myAge = (EditText) findViewById(R.id.myAge);
myBirthday = (EditText) findViewById(R.id.myBirthday);submitButton = (Button) findViewById(R.id.ok);
submitButton.setOnClickListener(new OnClickListener(){String inputMyName;
int inputMyAge;
String inputMyBirthday;
Student student = null;
@Override
public void onClick(View v) {
inputMyName = myTextView.getText().toString();
inputMyAge = Integer.valueOf(myAge.getText().toString());
inputMyBirthday = myBirthday.getText().toString();
student = new Student(inputMyName, inputMyAge, inputMyBirthday);
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("STUDENT", student);
startActivity(intent);
}
});
}
}
SubActivity.java
package com.test;
import com.test.vo.Student;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class SubActivity extends Activity {
Button backBtn;
Bundle bundle;
TextView resultMyNameTextView;
TextView resultMyAgeTextView;
TextView resultMyBirthdayTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
bundle = this.getIntent().getExtras();
Student student = bundle.getParcelable("STUDENT");
resultMyNameTextView = (TextView) findViewById(R.id.resultMyName);
resultMyAgeTextView = (TextView) findViewById(R.id.resultMyAge);
resultMyBirthdayTextView = (TextView) findViewById(R.id.resultMyBirthday);
resultMyNameTextView.setText(student.getName());
resultMyAgeTextView.setText(String.valueOf(student.getAge()));
resultMyBirthdayTextView.setText(student.getBirthday());
backBtn = (Button) findViewById(R.id.back);
backBtn.setOnClickListener(new OnClickListener(){@Override
public void onClick(View v) {
onBackPressed();
}
});
}}
main.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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="name : "
/>
<EditText
android:id="@+id/myName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sungsik"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="age : "
/>
<EditText
android:id="@+id/myAge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="30"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="birthday : "
/>
<EditText
android:id="@+id/myBirthday"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0810"
/>
</LinearLayout>
<Button
android:id="@+id/ok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit To SubActivity"
/>
</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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="name : "
/>
<TextView
android:id="@+id/resultMyName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="age : "
/>
<TextView
android:id="@+id/resultMyAge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="birthday : "
/>
<TextView
android:id="@+id/resultMyBirthday"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit To SubActivity"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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=".SubActivity" android:label="@string/app_name" />
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
[Android] Custom ProgressBar - 프로그래스바 모양 변경 (1) | 2011.05.06 |
---|---|
[Android] BroadcastReceiver 샘플테스트 (0) | 2011.05.06 |
[Android] Wifi 다루기 (0) | 2011.05.06 |
[Android] BroadcastReceiver에서 wifi / 3G 분기하기 (0) | 2011.05.06 |
진저브레드관련 - Android App 개발시 주의사항요청(CTS 승인이슈건) (0) | 2011.05.06 |
Recent Comments