티스토리 뷰

New - Other... 메뉴에서

 


 

1. 새로운 액티비티 생성

 

 

액티비티 클래스 이름 등록 ↑

 

AndroidManifest.xml 에 아래코드가 자동 등록된다  (추가된  액티비티 클래스) ↑

        <activity
            android:name=".ThirdActivity"
            android:label="@string/title_activity_third" >
        </activity>

 


 

2. 메인화면 - 호출버튼 생성

res/layout/activity_main.xml 에서 버튼 추가

 

    <Button
        android:id="@+id/buttonThirdA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:text="@string/thirdact" />      

MainActivity.java 에 버튼 이벤트 추가 및 액티비티 호출

 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //...
        Button buttonThirdA = (Button)findViewById(R.id.buttonThirdA);
        buttonThirdA.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Intent intentSubActivity = new Intent(MainActivity.this, ThirdActivity.class);
        		startActivity(intentSubActivity);
        	}
        });
        //...
    }

    

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">First</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">메세지 잠시 띄우기</string>
    <string name="daum">브라우저에 다음 실행 </string>
    <string name="tel">전화걸기 화면 실행</string>
    <string name="subact">서브액티비티 실행</string>
    <string name="title_activity_third">ThirdActivity</string>
    <string name="thirdact">세번째액티비티 실행</string>
</resources>


 

3. 추가된 액티비티 화면 - 액티비티 닫기버튼 및 이벤트 코드 추가

 

res/layout/thirdactivity.xml

 

<RelativeLayout 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: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="com.example.first.ThirdActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="세번째 액티비티" />

    <Button
        android:id="@+id/close_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="닫기" />

</RelativeLayout>

ThirdActivity.java 에 버튼이벤트 및 액티비티 닫기 코드

 

 //...
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thirdactivity);
		//추가한 코드
		Button close_button = (Button) findViewById(R.id.close_button);
		close_button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
 //...