🧵 크로노미터
💫 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">
<Chronometer
android:id="@+id/chrono"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:gravity="center_horizontal"
android:textColor="#6495ED"></Chronometer>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="START"
android:id="@+id/bt_start"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/bt_stop"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RESET"
android:id="@+id/bt_reset"></Button>
</LinearLayout>
💫 java 코드
📌SystemClock.elapsedRealtime을 꼭 추가해준다!! 그래야지 디바이스가 부팅된 시점부터 시간이 표시되는 것을 막을 수 있다.
package com.example.w12_time;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
Chronometer chrono;
Button bt_start, bt_stop, bt_reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chrono = (Chronometer) findViewById(R.id.chrono);
bt_start = (Button) findViewById(R.id.bt_start);
bt_stop = (Button) findViewById(R.id.bt_stop);
bt_reset = (Button) findViewById(R.id.bt_reset);
bt_stop.setEnabled(false);
bt_reset.setEnabled(false);
bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
bt_start.setEnabled(false);
bt_stop.setEnabled(true);
}
});
bt_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chrono.stop();
bt_reset.setEnabled(true);
bt_stop.setEnabled(false);
}
});
bt_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chrono.setBase(SystemClock.elapsedRealtime());
bt_reset.setEnabled(false);
bt_start.setEnabled(true);
}
});
}
}
🧵 타임피커와 캘린더뷰
📏기억해야 할 키워드
- (캘린더뷰) 년/월/일 중 '월'은 Month+1
- SetOnDateChangeListner (날짜 클릭하는 것 기다리고 있다가 클릭하면 감지),
- (위) new CalendarView.OnDateChangeListener()와 세트
- tvHour.setText(Integer.toString(tPicker.getHour())); <--int형을 문자열로 변환하여 표시하기
💫 완성화면
- 예약 시작 버튼을 누르면 Chronometer가 작동되면서 시간이 흘러감.
- 처음에는 아래에 아무것도 보이지 않다가, '날짜설정' 라디오 버튼을 누르면 캘린더뷰가 보이고 / '시간설정' 라디오 버튼을 누르면 타임피커(스피너 모드)가 나옴.
- 날짜와 시간 선택 후 예약완료 버튼을 누르면 Chronometer는 멈추고 사용자가 선택한 날짜/시간을 받아와 아래 표시함.
💫 xml 코드
<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"
android:baselineAligned="false"
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="예약 시작" />
<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="0dp"
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:timePickerMode="spinner"
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>
💫 java 코드
package com.example.w12_time;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
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
public 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() {
public void onClick(View v) {
calView.setVisibility(View.VISIBLE);
tPicker.setVisibility(View.INVISIBLE);
}
});
rdoTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
calView.setVisibility(View.INVISIBLE);
tPicker.setVisibility(View.VISIBLE);
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
chrono.setTextColor(Color.MAGENTA);
}
});
btnEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
chrono.stop();
tvHour.setText(Integer.toString(tPicker.getHour()));
tvMinute.setText(Integer.toString(tPicker.getMinute()));
tvYear.setText(Integer.toString(selectYear));
tvMonth.setText(Integer.toString(selectMonth));
tvDay.setText(Integer.toString(selectDay));
}
});
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;
}
});
}
}