CalendarView,TimePicker 사용예제: 선택된 날짜 표시하기
2019. 3. 19. 12:10ㆍAndriod
# *.xml 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:baselineAligned="false" android:layout_width="fill_parent" android:layout_height="fill_parent"> <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:layout_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> | cs |
# * .java 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | package com.example.new0318; 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; 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.ex06_4); setTitle("예 약"); /* 변수 선인 및 xml요소 연결 */ 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 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.setTextColor(Color.RED); chrono.start(); } }); /* 종료(예약종료)버튼 => 크르노미터 종료, 선택된 시간,날짜를 텍스트 뷰에 나타나도록 설정 */ btnEnd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 크로노미터 종료 chrono.stop(); chrono.setTextColor(Color.BLUE); // java.util.Calendar curDate = java.util.Calendar.getInstance(); // curDate.setTimeInMillis(calView.getDate()); // 날짜 tvYear.setText("0"+selectYear); tvMonth.setText("0"+selectMonth); tvDay.setText("0"+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; } }); }//onCreate } | cs |
'Andriod' 카테고리의 다른 글
간단한 브라우저 만들기 (0) | 2019.03.19 |
---|---|
TapHost : 탭호스트 화면전환 (0) | 2019.03.19 |
Bar :ProgressBar,SeekBar,RatingBar (0) | 2019.03.19 |
ViewFlipper : 화면 전환 (0) | 2019.03.19 |
자동완성텍스트뷰,멀티자동완성테스트뷰 (0) | 2019.03.19 |