반응형
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"
tools:context=".MainActivity"
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="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:showWeekNumber="false"/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</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:id="@+id/tv1"
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:id="@+id/tv2"
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:id="@+id/tv3"
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:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시"/>
<TextView
android:id="@+id/tvmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"/>
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="분 예약됨"/>
</LinearLayout>
</LinearLayout>
main.java
package com.cookandroid.day_ex01;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
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.timePicker);
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.tvmin);
tPicker.setVisibility(View.INVISIBLE);
calView.setVisibility(View.INVISIBLE);
rdoCal.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
tPicker.setVisibility(View.INVISIBLE);
calView.setVisibility(View.VISIBLE);
}
});
rdoTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tPicker.setVisibility(View.VISIBLE);
calView.setVisibility(View.INVISIBLE);
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
chrono.setTextColor(Color.RED);
}
});
btnEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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;
}
});
}
}
반응형
'Programming > Android' 카테고리의 다른 글
안드로이드 textview null값 체크하기, 안드로이드 버튼 디자인 사이트 (0) | 2021.08.17 |
---|---|
안드로이드 친구목록만들기 (리사이클러뷰) (0) | 2021.08.17 |
안드로이드 리사이클러뷰(RecyclerView) 예제 (0) | 2021.08.17 |
안드로이드 리스트뷰(listview) 예제 (0) | 2021.08.17 |
안드로이드프로그래밍 RecyclerView 예제 (안드로이드 예제) (0) | 2021.08.16 |