๋ชฉ์ฐจ
https://stackoverflow.com/questions/54784149/timepicker-minute-intervals
package com.example.test1026
import android.content.res.Resources
import android.os.Bundle
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.TimePicker
import androidx.appcompat.app.AppCompatActivity
import com.example.test1026.databinding.ActivityTimeBinding
class TimeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_time)
//๊ฐ์ฒด ์ค์
val timePicker: TimePicker = findViewById(R.id.timePicker)
val timeText: TextView = findViewById(R.id.time_text)
//TimePicker ๊ฐ ๋ณ๊ฒฝ ์ด๋ฒคํธ
timePicker.setOnTimeChangedListener { timePicker, hourOfDay, minute ->
setTimePickerInterval(timePicker)
//๋ณ๊ฒฝ ๋ ๊ฐ Textview์ ๋ด๊ธฐ
timeText.text = "${hourOfDay}์ ${minute}๋ถ"
}//setOnTimeChangedListener
}//onCreate
}//AppCompatActivity
private fun setTimePickerInterval(timePicker: TimePicker) {
try {
val TIME_PICKER_INTERVAL = 5
val minutePicker = timePicker.findViewById(
Resources.getSystem().getIdentifier(
"minute", "id", "android"
)
) as NumberPicker
minutePicker.minValue = 0
minutePicker.maxValue = 60 / TIME_PICKER_INTERVAL - 1
val displayedValues: MutableList<String> = ArrayList()
var i = 0
while (i < 60) {
displayedValues.add(String.format("%02d", i))
i += TIME_PICKER_INTERVAL
}
minutePicker.displayedValues = displayedValues.toTypedArray()
} catch (e: Exception) {
}
} //setTimePickerInterval()
์๊ฐ 5๋ถ ๊ฐ๊ฒฉ์ผ๋ก ์์ ์๋ฃ
ํ๋ฉด์ ํ์๋๋ ์๊ฐ ์์ ํ๊ธฐ
package com.example.test1026
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.widget.NumberPicker
import android.widget.TextView
import android.widget.TimePicker
import androidx.appcompat.app.AppCompatActivity
import com.example.test1026.databinding.ActivityTimeBinding
import kotlinx.android.synthetic.main.activity_time.*
import java.util.Calendar
import java.util.Calendar.getInstance
import java.util.logging.Logger.global
class TimeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_time)
//๊ฐ์ฒด ์ค์
val timePicker: TimePicker = findViewById(R.id.timePicker)
setTimePickerInterval(timePicker)
//TimePicker ๊ฐ ๋ณ๊ฒฝ ์ด๋ฒคํธ
timePicker.setOnTimeChangedListener { timePicker, hourOfDay, minute ->
timeText.text="${hourOfDay}์ ${minute*5}๋ถ"
}//setOnTimeChangedListener
}//onCreate
}//AppCompatActivity
// ์๊ฐ ๊ฐ๊ฒฉ์ 5๋ถ ๋จ์๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์ setTimePickerInterval()
private fun setTimePickerInterval(timePicker: TimePicker) {
lateinit var binding: ActivityTimeBinding
try {
val TIME_PICKER_INTERVAL = 5
val minutePicker = timePicker.findViewById(
Resources.getSystem().getIdentifier(
"minute", "id", "android"
)
) as NumberPicker
minutePicker.minValue = 0
minutePicker.maxValue = 60 / TIME_PICKER_INTERVAL - 1
val displayedValues: MutableList<String> = ArrayList()
var i = 0
while (i < 60) {
displayedValues.add(String.format("%02d", i))
i += TIME_PICKER_INTERVAL
}
minutePicker.displayedValues = displayedValues.toTypedArray()
} //try
catch (e: Exception) {
}
} //setTimePickerInterval()
์์ฒญ ๋น๋น ๋๋ฌ๊ฐ๋๋ฐ ๊ฒฐ๊ตฌ ์ด๋ ๊ฒ ํด์ ํด๊ฒฐํ๋ค
timeText.text="${hourOfDay}์ ${minute*5}๋ถ"
728x90