2swan
Android 액티비티 데이터 전달 예제 본문
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity6"
android:orientation="vertical">
<Button
android:id="@+id/btnDataInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="데이터입력"
android:textSize="30dp"/>
<Button
android:id="@+id/btnDataOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="데이터 출력"
android:textSize="30dp"/>
<Button
android:id="@+id/btnDataStu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="학생 정보 입력"
android:textSize="30dp"/>
</LinearLayout>
input.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity6_input"
android:layout_gravity="center"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp">
<TableRow android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름"/>
<EditText
android:id="@+id/editName"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:hint="이름"/>
</TableRow>
<TableRow android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나이"/>
<EditText
android:id="@+id/editAge"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:hint="나이"/>
</TableRow>
<TableRow android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전화번호"/>
<EditText
android:id="@+id/editPhone"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:hint="전화번호"/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnInputCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력완료"
android:textSize="15sp"/>
</LinearLayout>
output.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity6_output"
android:orientation="vertical">
<TextView
android:id="@+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력 결과 : "
android:textSize="25sp"/>
<TextView
android:id="@+id/tvDisplay2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="학생 입력 결과 : "
android:textSize="25sp"/>
<Button
android:id="@+id/btnMainBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="돌아가기"
android:textSize="15sp"
android:layout_margin="10dp"/>
</LinearLayout>
stuput.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity6_Stu"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="학번"
android:textSize="25sp"/>
<EditText
android:id="@+id/editSnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="학번"
android:layout_weight="1"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름"
android:textSize="25sp"/>
<EditText
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="이름"
android:layout_weight="1"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전공"
android:textSize="25sp"/>
<EditText
android:id="@+id/editMajor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="전공"
android:layout_weight="1"
android:textSize="25sp"/>
</LinearLayout>
<Button
android:id="@+id/btnStuHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="학생입력완료"
android:textSize="30sp"/>
</LinearLayout>
Student.java (class)
public class Student implements Serializable {
private int sno;
private String name;
private String major;
public Student(){
}
public Student(int sno, String name, String major) {
this.sno = sno;
this.name = name;
this.major = major;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
main.activity
public class MainActivity6 extends AppCompatActivity {
private String name, age, phone;
private Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
Button btnDataInput = findViewById(R.id.btnDataInput);
Button btnDataOut = findViewById(R.id.btnDataOut);
Button btnDataStu = findViewById(R.id.btnDataStu);
//입력
btnDataInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity6_input.class);
// startActivity(intent);
startActivityForResult(intent,0);
}
});
//출력
btnDataOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity6_output.class);
intent.putExtra("name",name);
intent.putExtra("age",age);
intent.putExtra("phone",phone);
intent.putExtra("student",student);
startActivity(intent);
}
});
//학생 정보 입력
btnDataStu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity6_Stu.class);
startActivityForResult(intent,0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
name = data.getStringExtra("name");
age = data.getStringExtra("age");
phone = data.getStringExtra("phone");
return;
}
if(resultCode == 10){
student = (Student) data.getSerializableExtra("student");
return;
}
}
}
input.activity
public class MainActivity6_input extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity6_input);
EditText editName = findViewById(R.id.editName);
EditText editAge = findViewById(R.id.editAge);
EditText editPhone = findViewById(R.id.editPhone);
Button btnInputCom = findViewById(R.id.btnInputCom);
btnInputCom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity6.class);
intent.putExtra("name", editName.getText().toString());
intent.putExtra("age", editAge.getText().toString());
intent.putExtra("phone", editPhone.getText().toString());
setResult(RESULT_OK,intent);
finish();
}
});
}
}
output.activity
public class MainActivity6_output extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity6_output);
setTitle("output 액티비티");
Button btnMainBack = findViewById(R.id.btnMainBack);
TextView tvDisplay = findViewById(R.id.tvDisplay);
TextView tvDisplay2 = findViewById(R.id.tvDisplay2);
// intent 로 부터 받아오기
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String age = intent.getStringExtra("age");
String phone = intent.getStringExtra("phone");
tvDisplay.setText("데이터 : "+name+" // "+ age + " // " + phone+"");
//// Student 객체로 받기
Student student = (Student) intent.getSerializableExtra("student");
if(student != null) {
tvDisplay2.setText("학생 객체 : " + student.getSno() + " : " + student.getName() + " : " + student.getMajor());
}
btnMainBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(getApplicationContext(), MainActivity6.class);
finish();
}
});
}
}
stu.activity
public class MainActivity6_Stu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity6_stu);
EditText editSnum = findViewById(R.id.editSnum);
EditText editName = findViewById(R.id.editName);
EditText editMajor = findViewById(R.id.editMajor);
Button btnStuHome = findViewById(R.id.btnStuHome);
btnStuHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Student student = new Student();
student.setSno(Integer.parseInt(editSnum.getText().toString()));
student.setName(editName.getText().toString());
student.setMajor(editMajor.getText().toString());
Intent intent = new Intent();
intent.putExtra("student",student);
setResult(10,intent);
finish();
}
});
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
Android 액티비티 데이터 전달 예제 launcher (0) | 2023.08.04 |
---|---|
Android 액티비티 데이터 전달 예제 launcher (더하기) (0) | 2023.08.04 |
Android 화면 전환 (0) | 2023.08.03 |
Android Rating Bar 증가 (0) | 2023.08.03 |
Android 투표 앱 예제 (0) | 2023.08.03 |