와챠의 우당탕탕 개발 기록장
[안드로이드] 직접 풀어보기 6-1 본문
반응형
직접 풀어보기 6-1
실습[6-1]을 다음과 같이 수정하라
- 캘린더 뷰 대신에 데이트 피커를 사용하여 날짜를 설정한다.
- <예약 시작>과 <예약 완료>를 없앤다. 대신 <예약 시작> 기능은 크로노미터를 클릭하면 동작하게 하고,
- <예약 완료> 기능은 화면 하단의 연도(0000년)를 롱클릭하면 동작하게 한다.
- 크로노미터를 클릭하기 전에는 라디오버튼, 데이트피커, 타임피커가 안 보이도록 설정하고, 크로노미터를 클릭하면 라디오 버튼이 나타나게 한다. 그리고 화면 하단의 연도를 롱클릭하면 라디오 버튼, 데이트 피커, 타임 피커가 다시 사라지게 한다.

MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mytestsdk26 | |
import android.graphics.Color | |
import android.os.Build | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.os.CountDownTimer | |
import android.os.SystemClock | |
import android.view.View | |
import android.widget.* | |
import org.w3c.dom.Text | |
class MainActivity : AppCompatActivity() { | |
lateinit var chrono : Chronometer | |
lateinit var rdoG : RadioGroup | |
lateinit var rdoDate : RadioButton | |
lateinit var rdoTime : RadioButton | |
lateinit var dPiker : DatePicker | |
lateinit var tPicker : TimePicker | |
lateinit var tvYear : TextView | |
lateinit var tvMonth : TextView | |
lateinit var tvDay : TextView | |
lateinit var tvHour : TextView | |
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) | |
chrono = findViewById<Chronometer>(R.id.chrono) | |
rdoG = findViewById<RadioGroup>(R.id.rdoG) | |
rdoDate = findViewById<RadioButton>(R.id.rdoDate) | |
rdoTime = findViewById<RadioButton>(R.id.rdoTime) | |
dPiker = findViewById<DatePicker>(R.id.dPiker) | |
tPicker = findViewById<TimePicker>(R.id.tPicker) | |
tvYear = findViewById<TextView>(R.id.tvYear) | |
tvMonth = findViewById<TextView>(R.id.tvMonth) | |
tvDay = findViewById<TextView>(R.id.tvDay) | |
tvHour = findViewById<TextView>(R.id.tvHour) | |
tvMinute = findViewById<TextView>(R.id.tvMinute) | |
// 처음에는 라디오 버튼/데이트 피커/타임피커 둘 다 안 보이게 설정 | |
rdoG.visibility = View.INVISIBLE | |
dPiker.visibility = View.INVISIBLE | |
tPicker.visibility = View.INVISIBLE | |
//날짜 설정 라디오 버튼 눌렀을 때 데이트 피커 보이게 설정 | |
rdoDate.setOnClickListener{ | |
dPiker.visibility = View.VISIBLE | |
} | |
// 시간 설정 라디오 버튼 눌렀을 때 타임피커 보이게 설정 | |
rdoTime.setOnClickListener{ | |
tPicker.visibility = View.VISIBLE | |
} | |
// 데이트 피커에서 날짜가 설정 되었을 때 | |
dPiker.setOnDateChangedListener { datePicker, i, i2, i3 -> // SDK 26이상부터 사용할 수 있음 | |
selectYear = i | |
selectMonth = i2 + 1 | |
selectDay = i3 | |
} | |
// 예약 시작 + 라디오 버튼 보이게 | |
chrono.setOnClickListener { | |
rdoG.visibility = View.VISIBLE | |
chrono.base = SystemClock.elapsedRealtime() | |
chrono.start() | |
chrono.setTextColor(Color.RED) | |
} | |
// 예약 완료 + 예약 날짜와 시간 표시 | |
tvYear.setOnLongClickListener { | |
//예약 완료 | |
chrono.stop() | |
chrono.setTextColor(Color.BLUE) | |
// 시간 설정 | |
tvYear.text = selectYear.toString() | |
tvMonth.text = selectMonth.toString() | |
tvDay.text = selectDay.toString() | |
tvHour.text = tPicker.hour.toString() // SDK 23이상부터 사용할 수 있음 | |
tvMinute.text = tPicker.minute.toString() | |
// 라디오버튼/데이트피커/타임피커 안 보이게 | |
rdoG.visibility = View.INVISIBLE | |
dPiker.visibility = View.INVISIBLE | |
tPicker.visibility = View.INVISIBLE | |
false | |
} | |
} | |
} |
activity_main.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<Chronometer | |
android:id="@+id/chrono" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:format="예약에 걸린 시간 %s" | |
android:gravity="center" | |
android:textSize="20dp" | |
android:background="#CCCCCC"/> | |
<RadioGroup | |
android:id="@+id/rdoG" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<RadioButton | |
android:id="@+id/rdoDate" | |
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" | |
android:orientation="horizontal"> | |
<TimePicker | |
android:id="@+id/tPicker" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:timePickerMode="spinner" | |
android:layout_weight="1"/> | |
<DatePicker | |
android:id="@+id/dPiker" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_weight="1"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#CCCCCC" | |
android:gravity="center"> | |
<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> |
아놔 왜 자꾸 오류가 나나 했더니 데이트 피커는 SDK 26이상 부터랜다...
한 10분 날림ㅋ
참고로 저 날짜는... 내가 물슈를 데려올 날이다 ㅎ.ㅎ
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 5장 연습문제 5번 (0) | 2021.03.27 |
---|---|
[안드로이드] 5장 연습문제 4번 (0) | 2021.03.27 |
[안드로이드] 직접 풀어보기 6-2(뷰플리퍼로 자동 사진 넘기기) (0) | 2021.03.27 |
[안드로이드] 직접 풀어보기 5-4 (0) | 2021.03.20 |
[안드로이드] 직접 풀어보기 5-3 (0) | 2021.03.20 |