Android Application
Kotlin 고급 위젯 - 날짜와 시간 관련 위젯 : 날짜 / 시간 예약 앱 만들기
작성자 임베디드코리아
작성일23-09-24 01:17
조회1,709회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
>
<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="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: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.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var chrono: Chronometer
private lateinit var btnStart: Button
private lateinit var btnEnd: Button
private lateinit var rdoCal: RadioButton
private lateinit var rdoTime: RadioButton
private lateinit var calView: CalendarView
private lateinit var tPicker: TimePicker
private lateinit var tvYear: TextView
private lateinit var tvMonth: TextView
private lateinit var tvDay: TextView
private lateinit var tvHour: TextView
private lateinit var tvMinute: TextView
var selectYear: Int=0
var selectMonth: Int=0
var selectDay: Int=0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setTitle("시간예약");
btnStart = findViewById(R.id.btnStart)
btnEnd = findViewById(R.id.btnEnd)
chrono= findViewById(R.id.chronometer1)
rdoCal = findViewById(R.id.rdoCal)
rdoTime = findViewById(R.id.rdoTime)
tvYear = findViewById(R.id.tvYear)
tvMonth = findViewById(R.id.tvMonth)
tvDay = findViewById(R.id.tvDay)
tvHour = findViewById(R.id.tvHour)
tvMinute = findViewById(R.id.tvMinute)
calView = findViewById(R.id.calendarView1)
tPicker = findViewById(R.id.timePicker1)
//라디오버튼 클릭 시 캘린더뷰와 타임피커 중 하나씩만 보이게 클릭 이벤트 리스너
rdoCal.setOnClickListener {
tPicker.setVisibility(View.INVISIBLE)
calView.setVisibility(View.VISIBLE)
}
rdoTime.setOnClickListener {
tPicker.setVisibility(View.VISIBLE)
calView.setVisibility(View.INVISIBLE)
}
btnStart.setOnClickListener {
chrono.setBase(SystemClock.elapsedRealtime())
chrono.start();
chrono.setTextColor(Color.RED);
}
btnEnd.setOnClickListener {
chrono.stop();
chrono.setTextColor(Color.BLUE);
//예약완료 클릭 시 그 내용이 텍스트뷰에 채워지게 함
tvYear.text = selectYear.toString()
tvMonth.text = selectMonth.toString()
tvDay.text = selectDay .toString()
tvHour.text = tPicker.currentHour.toString()
tvMinute.text = tPicker.currentMinute.toString()
}
calView.setOnDateChangeListener { view, year, month, dayOfMonth ->
selectYear = year
selectMonth = month+1
selectDay = dayOfMonth
}
}
}