IT/Android

android - FrameLayout , Chronometer, TimePicker, CalendarView

노마드오브 2018. 9. 10. 22:27

파일명 : activity_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="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Chronometer
android:id="@+id/chronometer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format=" 예약에 걸린 시간 %s "
android:gravity="center"
android:textSize="20dp" />

<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="예약시작" />
</LinearLayout>

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/rdoCal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="날짜 설정 (캘린더뷰)" />
<RadioButton
android:id="@+id/rdoTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시간 설정" />
</RadioGroup>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >

<CalendarView
android:id="@+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:showWeekNumber="false" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC" >

<Button
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="예약완료" />

<TextView
android:id="@+id/tvYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="년" />
<TextView
android:id="@+id/tvMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="월" />
<TextView
android:id="@+id/tvDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="일" />

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

</LinearLayout>


파일명 : MainActivity.java

package com.example.it.myapplication6_4;

import android.graphics.Color;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Chronometer;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

Chronometer chrono;
Button btnStart, btnEnd;
RadioButton rdoCal, rdoTime;
CalendarView calView;
TimePicker tPicker;
TextView tvYear, tvMonth, tvDay, tvHour, tvMinute;
int selectYear, selectMonth, selectDay;

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

btnStart = (Button) findViewById(R.id.btnStart);
btnEnd = (Button) findViewById(R.id.btnEnd);

chrono = (Chronometer) findViewById(R.id.chronometer1);

rdoCal = (RadioButton) findViewById(R.id.rdoCal);
rdoTime = (RadioButton) findViewById(R.id.rdoTime);

tPicker = (TimePicker) findViewById(R.id.timePicker1) ;
calView = (CalendarView) findViewById(R.id.calendarView1) ;

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);

tPicker.setVisibility(View.INVISIBLE);
calView.setVisibility(View.INVISIBLE);

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

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

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

btnEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chrono.stop();
chrono.setTextColor(Color.BLUE);

tvYear.setText(Integer.toString(selectYear));
tvMonth.setText(Integer.toString(selectMonth));
tvDay.setText(Integer.toString(selectDay));

tvHour.setText(Integer.toString(tPicker.getCurrentHour()));
tvMinute.setText(Integer.toString(tPicker.getCurrentMinute()));
}
});

calView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange
(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
selectYear = year;
selectMonth = month + 1;
selectDay = dayOfMonth;
}
});

}
}