๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

์‹œ๊ฐ„ ๊ฐ„๊ฒฉ

by hyeonha 2022. 12. 1.

๋ชฉ์ฐจ

    https://stackoverflow.com/questions/54784149/timepicker-minute-intervals

     

    TimePicker Minute Intervals

    I have built a view with a Date Picker and a Time Picker in spinner format. Everything seems to be working good except I can't figure out how to set the minutes to be in 5 minute intervals? I hav...

    stackoverflow.com

    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