프로그래밍

[안드로이드 자바] 예약 어플 만들기(Chronometer, TimePicker, DatePicker, CalendarView) 본문

안드로이드/안드로이드 스튜디오

[안드로이드 자바] 예약 어플 만들기(Chronometer, TimePicker, DatePicker, CalendarView)

시케 2022. 5. 17. 10:58
728x90
반응형

디자인

 

프로젝트 설명

TimePicker를 이용한 예약 프로그램

이전 포스팅과 거의 같다

2022.05.17 - [안드로이드 스튜디오] - [안드로이드 자바] 타임피커, 캘린더뷰를 이용한 예약 시스템 만들기(Timpicker, CalendarView)

 

...

타이머(Chronometer)를 누르면 붉은색으로 변하며 시작된다

날짜선택/시간선택을 라디오버튼을 이용하여 날짜는 캘린더뷰와 데이트피커, 시간은 스피너로 선택

연도를 오래 누를시(롱클릭) 예약이 종료되며 시간을 나타냄

 

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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Chronometer
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Chrono"
            android:format="예약 소요 시간 : %s"
            android:textSize="20dp"
            android:gravity="center"/>
    </LinearLayout>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RdoCal"
            android:text="날짜선택"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RdoTime"
            android:text="시간선택"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <DatePicker
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:datePickerMode="spinner"
                android:id="@+id/DatePicker1"/>
            <TimePicker
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:timePickerMode="spinner"
                android:id="@+id/TPicker"/>
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#cccccc">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TvYear"
            android:text=" 0000"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="년"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TvMonth"
            android:text=" 00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="월"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TvDay"
            android:text=" 00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="일"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TvHour"
            android:text=" 00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="시"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TvMinute"
            android:text=" 00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="분에 예약됨"/>
    </LinearLayout>

</LinearLayout>

 

java

package com.example.reservation;

import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    Chronometer chrono;
    RadioButton rdoCal, rdoTime;
    DatePicker datePicker1;
    TimePicker tPicker;
    TextView tvYear, tvMonth, tvDay, tvHour, tvMinute;

    int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("시간 예약");

        chrono = (Chronometer)findViewById(R.id.Chrono);
        rdoCal = (RadioButton)findViewById(R.id.RdoCal);
        rdoTime = (RadioButton)findViewById(R.id.RdoTime);
        datePicker1 = (DatePicker) findViewById(R.id.DatePicker1);
        tPicker = (TimePicker)findViewById(R.id.TPicker);
        tvYear = (TextView)findViewById(R.id.TvYear);
        tvMonth = (TextView)findViewById(R.id.TvMonth);
        tvDay = (TextView)findViewById(R.id.TvDay);
        tvHour = (TextView)findViewById(R.id.TvHour);
        tvMinute = (TextView)findViewById(R.id.TvMinute);

        rdoCal.setVisibility(View.INVISIBLE);
        rdoTime.setVisibility(View.INVISIBLE);
        datePicker1.setVisibility(View.INVISIBLE);
        tPicker.setVisibility(View.INVISIBLE);

        chrono.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chrono.setBase(SystemClock.elapsedRealtime());
                chrono.start();
                chrono.setTextColor(Color.RED);

                rdoCal.setVisibility(View.VISIBLE);
                rdoTime.setVisibility(View.VISIBLE);
            }
        });

        rdoCal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker1.setVisibility(View.VISIBLE);
                tPicker.setVisibility(View.INVISIBLE);
            }
        });

        rdoTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker1.setVisibility(View.INVISIBLE);
                tPicker.setVisibility(View.VISIBLE);
            }
        });

        tvYear.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                chrono.stop();
                chrono.setTextColor(Color.BLUE);

                tvYear.setText(Integer.toString(datePicker1.getYear()));
                tvMonth.setText(Integer.toString(i + datePicker1.getMonth()));
                tvDay.setText(Integer.toString(datePicker1.getDayOfMonth()));
                tvHour.setText(Integer.toString(tPicker.getCurrentHour()));
                tvMinute.setText(Integer.toString(tPicker.getCurrentMinute()));

                rdoCal.setVisibility(View.INVISIBLE);
                rdoTime.setVisibility(View.INVISIBLE);
                datePicker1.setVisibility(View.INVISIBLE);
                tPicker.setVisibility(View.INVISIBLE);

                return false;
            }
        });

    }
}
728x90
반응형
Comments