안드로이드 스튜디오

[안드로이드 스튜디오] Intent 를 이용한 다른페이지 이동 / 페이지 이동시 앱이 튕길때 / 새로운 페이지 java와 xml 연동

yangcotton 2023. 8. 30. 15:42

전체 소스가 보고싶다면~

https://yangcottondev.tistory.com/36

 

[안드로이드 스튜디오] GCS 앱 만들기 - 전체소스

MainActivity.java package com.example.gcs; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.Layo

yangcottondev.tistory.com

 

만든앱에 목록추가 버튼을 누르면 페이지를 이동시키려한다.

 

1. intent 를 사용하여 버튼 클릭시 페이지 이동

//다른 페이지로 이동 버튼
        Button moveAddPage = (Button) findViewById(R.id.moveAddPage);
        moveAddPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),AddNewList.class);
                startActivity(intent);
            }
        });

나는 AddNewList.java 로 페이지 이동을 하려고한다. 각자 이동할 페이지의 파일명을 적어주면 된다.

버튼의 xml 필요한 사람이 있다면 ~

<Button
        android:id="@+id/moveAddPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#77CFF2"
        android:text="이동버튼"
        android:textSize="19sp"
        android:textStyle="bold"
        />

 

2. 페이지 이동시 앱이 튕길때

 AndroidManifest.xml 에 추가를 해줘야한다.

위치는 app > manifests > AndroidManifest.xml 에 있다.

AndroidManifest.xml 에 가보면 이미 MainActivity.java 는 등록되어 있을것이다. 그 아래 똑같이 activity를 만들어주고

<activity android:name=".AddNewList" ></activity>

이렇게 이동할 페이지 파일을 추가해주면 된다.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        
        <activityandroid:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddNewList" ></activity>
    </application>

</manifest>

혹시 activity 를 어디다 넣야할지 모를까 예시로 넣었다

그냥 맨 아래 추가하면 된다.

application 안에 들어가게~

 

 

3. 새로운 페이지 java와 xml 연동

새로운 페이지를 만들고 java와 xml 연동을 하고싶다면

만든 java 파일엔

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.만든xml이름);
    }

setContentView(R.layout.newxml); 로 추가해주고

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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".만든자바파일이름">


</LinearLayou>

tools:context 로 추가해준다.