본문 바로가기
코리아 IT아카데미/android

7일차 | 시크릿 다이어리, 만든 앱 폰으로 보기

by Sharon kim 2022. 2. 22.

diary액티비티는 파일만 만듦, 내일 함

 

개인 디자인 해보기

 


activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/main_backgroud_color"
    tools:context=".MainActivity2">

    <TextView
        android:fontFamily="@font/cookierun_bold"
        android:textStyle="bold"
        android:textSize="30sp"
        android:id="@+id/titleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:text="@string/str_secret"
        app:layout_constraintBottom_toTopOf="@id/passwordLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:background="@color/password_background_color"
        android:padding="20dp"
        android:id="@+id/passwordLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/titleTv">

        <androidx.appcompat.widget.AppCompatButton
            android:layout_marginEnd="10dp"
            android:id="@+id/openButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginBottom="20dp"
            app:layout_constraintBottom_toTopOf="@id/changePasswordButton"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@id/numberPicker1"
            app:layout_constraintVertical_chainStyle="packed" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/changePasswordButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@id/openButton"
            app:layout_constraintStart_toStartOf="@id/openButton"
            app:layout_constraintTop_toBottomOf="@+id/openButton" />

        <NumberPicker
            android:layout_marginEnd="5dp"
            android:id="@+id/numberPicker1"
            android:layout_width="30dp"
            android:layout_height="120dp"
            android:background="#A5A6A6"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/openButton"
            app:layout_constraintEnd_toStartOf="@id/numberPicker2"
            app:layout_constraintTop_toTopOf="parent" />

        <NumberPicker
            android:layout_marginEnd="5dp"
            android:id="@+id/numberPicker2"
            android:layout_width="30dp"
            android:layout_height="120dp"
            android:background="#A5A6A6"
            app:layout_constraintEnd_toStartOf="@id/numberPicker3"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/numberPicker1"
            app:layout_constraintTop_toTopOf="parent" />

        <NumberPicker
            android:id="@+id/numberPicker3"
            android:layout_width="30dp"
            android:layout_height="120dp"
            android:background="#A5A6A6"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/numberPicker2"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

Main_Activity2.java

package com.example.mysecretdiaryapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {

    private NumberPicker numberPicker1;
    private NumberPicker numberPicker2;
    private NumberPicker numberPicker3;

    private Boolean changePasswordMode = false;

    private String tempValue = "123";

    private Button openButton;
    private Button changePasswordButton;

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

    private void initData() {
        numberPicker1 = findViewById(R.id.numberPicker1);
        numberPicker2 = findViewById(R.id.numberPicker2);
        numberPicker3 = findViewById(R.id.numberPicker3);
        numberPicker1.setMinValue(0);
        numberPicker1.setMaxValue(9);
        numberPicker2.setMinValue(0);
        numberPicker2.setMaxValue(9);
        numberPicker3.setMinValue(0);
        numberPicker3.setMaxValue(9);
        openButton = findViewById(R.id.openButton);
        changePasswordButton = findViewById(R.id.changePasswordButton);

    }

    private void addEventListener() {

        openButton.setOnClickListener(view -> {
            if (changePasswordMode) {
                Log.d("TAG", "비밀번호 변경모드");
            } else {
                Log.d("TAG", "실행 모드");
                String passwordFormUser = numberPicker1.getValue()
                        + numberPicker2.getValue()
                        + numberPicker3.getValue() + "";
                if (tempValue.equals(passwordFormUser)) {
                    Intent intent = new Intent(this, DiaryActivity.class);
                    startActivity(intent);
                }
            }
            //2. 비밀번호 모드가 맞다면
            //2-1 numberPicker 에 값 3개를 들고 와서
            //2-2 tempValue 값을 비교해서 맞다면
            //2-3 새로운 화면으로 전환 (인텐트)
        });

        changePasswordButton.setOnClickListener(v -> {
            // 1. 비밀번호 변경 모드로 코드를 작성
            String passwordFormUser = "" + numberPicker1.getValue()
                    + numberPicker2.getValue()
                    + numberPicker3.getValue();

            if (changePasswordMode) {
                // 번호를 저장하는 기능
                tempValue = passwordFormUser;
                changePasswordMode = false;
                changePasswordButton.setBackgroundColor(Color.WHITE);
                Toast.makeText(this, "변경완료", Toast.LENGTH_SHORT).show();
            } else {
                // 비밀번호가 맞는 체크하는 기능
                if (tempValue.equals(passwordFormUser)) {
                    changePasswordButton.setBackgroundColor(Color.RED);
                    changePasswordMode = true;
                } else {
                    showErrorAlertDialog();
                }
            }
        });
    }

    private void showErrorAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("실패")
                .setMessage("비밀번호가 잘못 되었습니다.")
                .setPositiveButton("닫기", (dialog, which) -> {
                    //동작하지 않음
                });
        builder.show();
    }
}

font 사용 방법 : res에 font 폴더 만들고 타입을 font, 폴더 안에 폰트명.ttp 파일 넣기 

<TextView 

android:fontFamily="@font/cookierun_bold" />

 


themes.xml, themes.xml(noght) 둘 다 NoActionBar 으로 수정

 

<style name="Theme.MySecretDiaryApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

<style name="Theme.MySecretDiaryApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">

깰끔


안드로이드 phone으로 앱 화면 전송

개발자 모드(설정 - 휴대전화 정보 - 소프트웨어 정보 - 빌드번호 연타)로 전환 이후,

 

방법1. 폰과 pc를 usb선으로 연결(연결 끊으면 끝)
폰 : 옵션 - 개발자 옵션 - USB 디버깅 활성화(on) - usb 설정 선택 중 PHP 선택 - USB 디버깅 허용

pc : 기기 선택

방법2. 만든 앱 다운받기

pc : (자신 작업 폴더)\app\build\intermediates\apk\debug >> app-debug.apk

핸드폰 파일관리자에 폴더 만들어서 app-debug.apk 넣기(usb연결 혹은 etc로 ...)

폰 : 앱 허용 부분을 활성화 하기(출처를 알 수없는 장치 허용 혹은 기기마다 다름)

참고 블로그 : https://blog.naver.com/PostView.nhn?blogId=dreve&logNo=221443385330