[ Do it! ] #7. 도전! - 로그인 화면과 메인화면 전환하기

ma.caron_g·2022년 2월 20일
0

Do it! - Android Studio

목록 보기
15/18
post-thumbnail

[ 📄 문 제 ]

대부분의 업무용 앱에서 필요한 로그인 화면과 메뉴 화면을 간단하게 만들고 두 화면 간을 전환하면서 토스트로 메시지를 띄워주도록 만들어보세요.


[ 📚 설 명 ]

  1. 로그인 화면과 메뉴 화면을 각각 액티비티로 만듭니다.
  2. 로그인 화면에는 하나의 버튼이 들어갑니다.
  3. 메뉴 화면에는 세 개의 버튼이 들어가도록 하고 각각 '고객 관리', '매출 관리', '상품 관리'라는 이름으로 표시합니다.
  4. 로그인 화면의 버튼을 누르면 메뉴 화면으로 이동합니다.
  5. 메뉴 화면의 버튼 중에서 하나를 누르면 로그인 화면으로 돌아온 후 선택된 메뉴의 이름을 토스트 메시지로 보여줍니다.

[ 💻 코 드 ]

[ edittext_draw.xml ]

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="150sp" android:height="30dp">
        <shape>
            <solid android:color="#ffffff"/>
            <stroke
                android:width="1.5dp"
                android:color="#aa33bbbb"/>
        </shape>
    </item>
</layer-list>

[ acitivity_main.xml ]

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/titleLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/mainColor">
        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="로그인 하기"
            android:textSize="25sp"
            android:textColor="#ffffff"
            android:textStyle="bold"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/txtLogin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="100dp"
        android:gravity="center">

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:backgroundTint="@color/mainTintColor"
            android:background="#aa33bbbb"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:text="로그인"/>

    </LinearLayout>

</LinearLayout>

[ acitivity_menu.xml ]

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/titleLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/mainColor">
        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="메인 메뉴"
            android:textSize="25sp"
            android:textColor="#ffffff"
            android:textStyle="bold"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="250dp"
        android:gravity="center">
        <Button
            android:id="@+id/btnCustomCare"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:background="@color/mainColor"
            android:layout_margin="20dp"
            android:text="고객 관리"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:backgroundTint="@color/mainTintColor"/>
        <Button
            android:id="@+id/btnSalesCare"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:background="@color/mainColor"
            android:layout_margin="20dp"
            android:text="매출 관리"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:backgroundTint="@color/mainTintColor"/>
        <Button
            android:id="@+id/btnProductCare"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:background="@color/mainColor"
            android:layout_margin="20dp"
            android:text="상품 관리"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:backgroundTint="@color/mainTintColor"/>
    </LinearLayout>
</LinearLayout>

[ MainActivity.java ]

package com.study.doit;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.nfc.Tag;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public static final int MENU_CODE_LOGIN = 10001;

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

        Button btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                moveActivity();
            }
        });
    }

    ActivityResultLauncher startActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {

        }
    });

    public void moveActivity(){
        Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
        startActivityResult.launch(intent);
    }

}

package com.study.doit;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MenuActivity extends AppCompatActivity {
    private static final String TAG = "MenuActivity";
    public static final int MENU_CODE_CUSTOM = 10002;
    public static final int MENU_CODE_SALES = 10003;
    public static final int MENU_CODE_PRODUCT = 10004;

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

        Button btnCustomCare, btnProductCare, btnSalesCare;

        btnCustomCare = findViewById(R.id.btnCustomCare);
        btnProductCare = findViewById(R.id.btnProductCare);
        btnSalesCare = findViewById(R.id.btnSalesCare);

        btnCustomCare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MenuActivity.this, MainActivity.class);
                Toast.makeText(getApplicationContext(), "고객 관리", Toast.LENGTH_SHORT).show();
                backActivity.launch(intent);
            }
        });

        btnSalesCare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MenuActivity.this, MainActivity.class);
                Toast.makeText(getApplicationContext(), "매출 관리", Toast.LENGTH_SHORT).show();
                backActivity.launch(intent);
            }
        });

        btnProductCare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MenuActivity.this, MainActivity.class);
                Toast.makeText(getApplicationContext(), "상품 관리", Toast.LENGTH_SHORT).show();
                backActivity.launch(intent);
            }
        });

    }
    ActivityResultLauncher backActivity = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {

        }
    });

}

[ 🖋 디자인 ]

좀 칙칙해서 UI를 꾸며보았습니다.
(작성자 성격이 꾸미면서 하는걸 좋아함.)


[ 💡 학습한 내용 ]

startActivityForResult를 이용하려 하였는데 안드로이드 스튜디오 업데이트로 deprecated 되면서 ActivityResultLauncherregisterForActivityResult를 이용하여 intent하는 방법을 배웠습니다.

기존 startActivity()를 사용하려 하였는데

참고할 점

각 화면은 액티비티로 만들고 화면 간 전환 시에는 startActivityForResult 메서드를 사용합니다.

라고 작성 돼 있어서 registerForActivityResult를 사용하였습니다.

하나의 클릭 메서드로 버튼을 누르는 경우마다 해당 메뉴를 토스트해주는 방법이 있을 거 같은데 registerForActivityResult를 잘 몰라서 각각의 버튼을 구현해주었습니다.

profile
다른 사람이 만든 것을 소비하는 활동보다, 내가 생산적인 활동을 하는 시간이 더 많도록 생활화 하자.

1개의 댓글

comment-user-thumbnail
2022년 2월 22일

잘보고갑니다^오^

답글 달기