티스토리 뷰
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnMoveToSub"
android:layout_width="400dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Move To SubActivity"
android:textSize="30dp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:background="#FF7F00" />
</LinearLayout>
activity_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:gravity="center"
android:orientation="vertical"
tools:context=".SubActivity">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_centerInParent="true"
tools:text="Name" />
<TextView
android:id="@+id/tvNickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvName"
android:layout_centerInParent="true"
tools:text="Nickname" />
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvNickname"
android:layout_centerInParent="true"
tools:text="Age" />
<TextView
android:id="@+id/tvFavoriteMovies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvAge"
android:layout_centerInParent="true"
android:singleLine="true"
android:ellipsize="end"
tools:text="FavoriteMovies" />
<TextView
android:id="@+id/tvPrivacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/tvFavoriteMovies"
android:layout_centerInParent="true"
tools:text="Privacy" />
<Button
android:id="@+id/btnMoveToMain"
android:layout_width="400dp"
android:layout_height="100dp"
android:textAllCaps="false"
android:text="Move To MainActivity"
android:textSize="30dp"
android:textStyle="bold|italic"
android:textColor="#FFFFFF"
android:background="#FF7F00"
android:layout_marginTop="100dp"
android:layout_below="@+id/tvPrivacy"
android:layout_centerInParent="true" />
</RelativeLayout>
Privacy.java
package com.jwsoft.javaproject;
import java.io.Serializable;
public class Privacy implements Serializable {
private String height;
private String weight;
public Privacy(String height, String weight) {
this.height = height;
this.weight = weight;
}
public void setHeight(String height) {
this.height = height;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getHeight() {
return height;
}
public String getWeight() {
return weight;
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnMoveToSub;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, SubActivity.class);
intent.putExtra("Name", "James Kim");
intent.putExtra("Nickname", "혀가 길지 않은 개발자");
intent.putExtra("Age", 30);
String[] movies = {"The Avengers", "Good Will Hunting", "Notting Hill"};
intent.putExtra("FavoriteMovies", movies);
intent.putExtra("Privacy", new Privacy("184", "84"));
btnMoveToSub = findViewById(R.id.btnMoveToSub);
btnMoveToSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
}
SubActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SubActivity extends AppCompatActivity {
private TextView tvName;
private TextView tvNickname;
private TextView tvAge;
private TextView tvFavoriteMovies;
private TextView tvPrivacy;
private Button btnMoveToMain;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
initializeView();
intent = new Intent(this, MainActivity.class);
btnMoveToMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
String name = getIntent().getStringExtra("Name");
String nickName = getIntent().getStringExtra("Nickname");
int age = getIntent().getIntExtra("Age", 0);
String[] moves = getIntent().getStringArrayExtra("FavoriteMovies");
Privacy privacy = (Privacy) getIntent().getSerializableExtra("Privacy");
tvName.setText(name);
tvNickname.setText(nickName);
tvAge.setText(Integer.toString(age));
String strMovies = "";
for (int i=0; i<moves.length; i++) {
strMovies += moves[i] + ", ";
}
tvFavoriteMovies.setText(strMovies);
tvPrivacy.setText(privacy.getHeight() + " / " + privacy.getWeight());
}
void initializeView() {
tvName = findViewById(R.id.tvName);
tvNickname = findViewById(R.id.tvNickname);
tvAge = findViewById(R.id.tvAge);
tvFavoriteMovies = findViewById(R.id.tvFavoriteMovies);
tvPrivacy = findViewById(R.id.tvPrivacy);
btnMoveToMain = findViewById(R.id.btnMoveToMain);
}
}
'Android > Java' 카테고리의 다른 글
[Java] Room (0) | 2020.07.20 |
---|---|
[Java] SQLite + Singleton Pattern (0) | 2020.07.18 |
[Java] CardView (0) | 2020.07.11 |
[Java] SQLite (0) | 2020.07.11 |
[Java] ViewModel (0) | 2020.07.10 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Design Pattern
- 안드로이드 #코틀린 #Android #Kotlin
- Architecture Pattern
- java
- DataBinding
- Intent
- 안드로이드
- handler
- Livedata
- 혀가 길지 않은 개발자
- 코틀린
- Kotlin
- ViewPager2
- JSONArray
- ViewModel
- CoordinatorLayout
- Android
- recyclerview
- 자바
- TabLayout
- ArrayList
- fragment
- James Kim
- coroutine
- JSONObject
- View
- MVVM
- activity
- Vue.js #Vue.js + javascript
- XML
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함