본문으로 바로가기

[안드로이드 스튜디오] 버튼클릭 화면전환(Intent)

 감성 자동제어 

 

안녕하세요! 오늘은 여러개의 액티비티로 구성된 앱에서 화면전환을 할때 어떻게 해야하는지에 대해서 알아보려고 합니다!! 저는 버튼을 통해 Intent를 이용해서 기능을 구현해 보려고 합니다.

어렵지 않으니 아래의 코드를 보시면 간단하게 따라하실 수 있습니다! 그럼 바로 시작하죠!!

 

 

Activity_main.xml(layout)

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
<?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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1페이지"
        android:textSize="50dp" />
 
 
    <Button
        android:id="@+id/button_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2페이지 전환"
        android:textSize="30dp" />
 
</LinearLayout>
cs

 

Activity_sub.xml(layout)

layout에 새로운 "activity_sub"만들고, 아래의 코드로 레이아웃을 구성합니다.

(Activity_main과 똑같고 아이디와 텍스트만 다름)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="2페이지"
        android:textSize="50dp" />
 
 
    <Button
        android:id="@+id/button_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1페이지 전환"
        android:textSize="30dp" />
    
</LinearLayout>
cs

 

MainActivity(Class)

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
package com.example.intent_page;
 
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 {
    
    Button btn_main;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn_main = findViewById(R.id.button_main);
 
        btn_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),SubActivity.class);
                startActivity(intent);
            }
        });
        
    }
}
cs

 

SubActivity(Class)

"SubActivity" 클래스를 만들어 주고 아래의 코드를 입력합니다!

(클래스를 만들면 빈화면이 생성될텐데 당황하시지 말고 아래의 코드를 천천히 입력해 주시면 됩니다!)

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
package com.example.intent_page;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class SubActivity extends Activity {
 
    Button btn_sub;
 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
 
        btn_sub = findViewById(R.id.button_sub);
 
        btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
 
    }
}
cs

 

AndroidManifest

"AndroidManifest" 에 아래의 코드를 추가합니다!

(아래의 코드를 추가시켜주지 않으면 앱이 강제로 종료됩니다...ㅜㅜ)

1
        <activity android:name=".SubActivity" />
cs

 

마무리

이상으로 안드로이드 스튜디오 버튼을 이용해서 액티비티 화면 전환을 마치도록 하겠습니다! 어렵지 않으니까 천천히 따라해보세요! 감사합니다!!

 

긴 글 읽느라 수고하셨습니다.

오늘도 일상 속 소소한 행복을 느끼길 바랍니다!

 

더 많은 정보

https://engineering-mino.tistory.com/