457
No
class MainActivity
package org.androidtown.myactivity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int ACTIVITY_MENU =1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Clicked(View v){
// 1. 첫번째 방법
//화면띄우기 안드로이드 에서는 Intent
/* Intent intent =new Intent(getApplicationContext(), MenuActivity.class);
intent.putExtra("title", "소녀시대");
//반환 값을 받고 싶을때
startActivityForResult(intent, ACTIVITY_MENU);
*/
//2. 두번째 방법
Intent intent =new Intent();
//ComponentName(패키기지이름, 패키지이름 + 클래스명)
ComponentName name =new ComponentName("org.androidtown.myactivity",
"org.androidtown.myactivity.MenuActivity");
intent.setComponent(name);
intent.putExtra("title", "소녀시대");
intent.putExtra("age", 20);
Person person01 =new Person("걸스데이", 30);
intent.putExtra("person" ,person01);
startActivityForResult(intent, ACTIVITY_MENU);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null){
String name =data.getStringExtra("name");
Toast.makeText(getApplicationContext(), "전달받은 값 " +name, Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
public void onButton2Clicked(View v){
Intent intent =new Intent(Intent.ACTION_DIAL, Uri.parse("tel:010-1000-1000 "));
startActivity(intent);
}
}
class MenuActivity
package org.androidtown.myactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MenuActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Intent intent=getIntent();
if(intent!=null){
String title =intent.getStringExtra("title");
Person person=(Person)intent.getSerializableExtra("person");
Toast.makeText(getApplicationContext(), "전달 받은값 : title ," + title
+ " , person :" +person.toString()
, Toast.LENGTH_SHORT).show();
}
}
public void onButton1Clicked(View v) {
Intent intent=new Intent();
intent.putExtra("name", "티아라");
setResult(RESULT_OK, intent);
//현재 화면을 지운다.
finish();
}
}
class Person
package org.androidtown.myactivity;
import java.io.Serializable;
/**
* Created by choi on 2017-04-01.
*/
public class Person implements Serializable {
String name;
int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.myactivity.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메이화면"
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<Button
android:text="새화면 띄우기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="58dp"
android:layout_centerHorizontal="true"
android:onClick="onButton1Clicked"
/>
<Button
android:text="전화걸기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:id="@+id/button2"
android:onClick="onButton2Clicked" />
</RelativeLayout>
R.layout.activity_menu
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.myactivity.MenuActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴화면"
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textColor="@color/colorAccent" />
<Button
android:text="돌아가기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="58dp"
android:layout_centerHorizontal="true"
android:onClick="onButton1Clicked"
/>
</RelativeLayout>
댓글 ( 4)
댓글 남기기