wasup

Android) 새 화면 추가해보기 본문

IT/Android

Android) 새 화면 추가해보기

wasupup 2021. 8. 3. 11:50
반응형

new project : hello_string

수정 : activity_main.xml 

수정 : MainActivity 

추가 : activity_new.xml 

추가 : NewActivity.java 

등록 : Manifests : Activity 

 

만들화면


activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="@string/app_name"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="@string/app_name2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="110dp"
        android:text="@string/app_name3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:text="버튼을 클릭하면 메시지가 출력됨"
        android:onClick="onButtonClicked"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:text="새 화면 띄우기"
        android:onClick="onButtonClicked2"/>

</RelativeLayout>

MainActivity.java

package com.example.hello_string;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //메서드 추가
    public void onButtonClicked(View v){
        Toast.makeText(getApplicationContext(),"버튼을 눌렀습니다",Toast.LENGTH_LONG).show();
    }

    //메서드 추가
    public void onButtonClicked2(View v){
        Intent intent=new Intent(getApplicationContext(),NewActivity.class);
        startActivity(intent);
    }
}

activity_new.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"
    >

    <Button
        android:id="@+id/backButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:text="되돌아가기"
        android:onClick="onBackButtonClicked"/>
	<!--xml이 추가되면 java파일도 추가되어야한다.-->
</LinearLayout>

.xml : 레이아웃 문서

.java : 액티비티 문서(java파일에서 xml문서를 인식함)

->

java파일이 Mainfest에 등록됨


NewActivity.java

package com.example.hello_string;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class NewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
    }

    //method-add
    public void onBackButtonClicked(View v){
        Toast.makeText(getApplicationContext(),"Back 버튼을 눌렀습니다",Toast.LENGTH_LONG).show();
        finish();
    }
}

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Hello_string">
        <activity
            android: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=".NewActivity">
        </activity>
    </application>

</manifest>

반응형
Comments