如何为计时器制作时间选择器对话框

时间:2017-07-18 04:14:04

标签: android android-timepicker

任何人都可以帮我制作时间戳对话框吗? 我制作了这样的完整布局,只需要知道如何制作选区 在listview的中心,我也在listview中设置了无限循环。

TimePickerDialog

这里是代码 - >

class TimePicker : AppCompatActivity(){

    private val timeSecondsAndMinutes = arrayOf("0", "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")

    private val timeHours = arrayOf("0", "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")

    private var hoursListView   : ListView? = null
    private var minutesListView : ListView? = null
    private var secondsListView : ListView? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_time_picker)


        InitializeListView()
    }

    private fun InitializeListView()
    {
        hoursListView = findViewById(R.id.HoursListView) as ListView
        minutesListView = findViewById(R.id.MinutesListView) as ListView
        secondsListView = findViewById(R.id.SecondsListView) as ListView

        hoursListView!!.setSelector(R.color.ListViewItemsColor)
        minutesListView!!.setSelector(R.color.ListViewItemsColor)
        secondsListView!!.setSelector(R.color.ListViewItemsColor)



        val hoursAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeHours)
        val secMinAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeSecondsAndMinutes)

        val circularHoursAdapter = CircularListAdapter(hoursAdapter)
        val circularMinSecAdapter = CircularListAdapter(secMinAdapter)

        hoursListView!!.adapter = circularHoursAdapter
        minutesListView!!.adapter = circularMinSecAdapter
        secondsListView!!.adapter = circularMinSecAdapter

        hoursListView!!.setSelection(544)
        minutesListView!!.setSelection(358)
        secondsListView!!.setSelection(358)

    }

2 个答案:

答案 0 :(得分:0)

单击edittext时使用此代码,从时间选择器稀释中选择时间。

eReminderTime.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    eReminderTime.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

答案 1 :(得分:0)

感谢大家,我找到了解决方案。

class TimePicker : AppCompatActivity(), AbsListView.OnScrollListener {

    private val timeSecondsAndMinutes = arrayOf("0", "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")

    private val timeHours = arrayOf("0", "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")

    private var hoursListView   : ListView? = null
    private var minutesListView : ListView? = null
    private var secondsListView : ListView? = null

    private var selectedItemHoursL : View? = null
    private var selectedItemMinutesL : View? = null
    private var selectedItemSecondsL : View? = null
    private var linearLayout : LinearLayout? = null

    private var oneTimeH : Boolean = true
    private var oneTimeM : Boolean = true
    private var oneTimeS : Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_time_picker)

        AdjustLayoutSize()
        InitializeListView()
    }

    private fun InitializeListView()
    {
        hoursListView = findViewById(R.id.HoursListView) as ListView
        minutesListView = findViewById(R.id.MinutesListView) as ListView
        secondsListView = findViewById(R.id.SecondsListView) as ListView

        hoursListView!!.setSelector(R.color.ListViewItemsColor)
        minutesListView!!.setSelector(R.color.ListViewItemsColor)
        secondsListView!!.setSelector(R.color.ListViewItemsColor)

        hoursListView!!.setOnScrollListener(this)
        minutesListView!!.setOnScrollListener(this)
        secondsListView!!.setOnScrollListener(this)

        val hoursAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeHours)
        val secMinAdapter = ArrayAdapter(this, R.layout.layout_for_listview_elements, timeSecondsAndMinutes)

        val circularHoursAdapter = CircularListAdapter(hoursAdapter)
        val circularMinSecAdapter = CircularListAdapter(secMinAdapter)

        hoursListView!!.adapter = circularHoursAdapter
        minutesListView!!.adapter = circularMinSecAdapter
        secondsListView!!.adapter = circularMinSecAdapter

        hoursListView!!.setSelection(544)
        minutesListView!!.setSelection(358)
        secondsListView!!.setSelection(358)
    }

    override fun onScroll(listViewSelected: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int)
    {
        if(firstVisibleItem != 0 && visibleItemCount != 0)
        {
            if (listViewSelected!!.id == hoursListView!!.id) {
                 if(oneTimeH){
                     SelectionNumberBackground(listViewSelected, hoursListView, R.drawable.selecteditem_hhss_bg)
                     oneTimeH = false
                }
            } else if (listViewSelected.id == minutesListView!!.id) {
                if(oneTimeM) {
                    SelectionNumberBackground(listViewSelected, minutesListView, R.drawable.selecteditem_mm_bg)
                    oneTimeM = false
                }
            } else if (listViewSelected.id == secondsListView!!.id) {
                if(oneTimeS) {
                    SelectionNumberBackground(listViewSelected, secondsListView, R.drawable.selecteditem_hhss_bg)
                    oneTimeS = false
                }
            }
        }
    }

    override fun onScrollStateChanged(p0: AbsListView?, p1: Int)
    {
        ChangeSelectionInList(p0)

        if (p1 == AbsListView.OnScrollListener.SCROLL_STATE_IDLE)
        {
            if(p0!!.id == hoursListView!!.id) {
                SelectionNumberBackground(p0, hoursListView,R.drawable.selecteditem_hhss_bg)
            }
            else if(p0.id == minutesListView!!.id){
                SelectionNumberBackground(p0,minutesListView,R.drawable.selecteditem_mm_bg)
            }
            else if(p0.id == secondsListView!!.id){
                SelectionNumberBackground(p0,secondsListView,R.drawable.selecteditem_hhss_bg)
            }
        }
    }

    private fun SelectionNumberBackground(p0 : AbsListView?, MainListView : ListView?, background : Int)
    {
        p0!!.setSelection(p0.firstVisiblePosition)
        val middleView = p0.firstVisiblePosition + 2
        val firstView = p0.firstVisiblePosition - MainListView!!.headerViewsCount
        val middleItem = middleView - firstView
        if (middleItem < 0 || middleItem >= p0.childCount) {
            return
        }
        val thirdView = p0.getChildAt(middleItem)
        thirdView.setBackgroundResource(background)
        if(p0.id == hoursListView!!.id){
            selectedItemHoursL = thirdView
        }
        else if(p0.id == minutesListView!!.id){
            selectedItemMinutesL = thirdView
        }
        else if(p0.id == secondsListView!!.id){
            selectedItemSecondsL = thirdView
        }
    }

    private fun ChangeSelectionInList(p0: AbsListView?)
    {
        if(p0!!.id == hoursListView!!.id) {
            if (selectedItemHoursL != null) {
                selectedItemHoursL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
        else if(p0.id == minutesListView!!.id){
            if(selectedItemMinutesL != null){
                selectedItemMinutesL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
        else if(p0.id == secondsListView!!.id){
            if(selectedItemSecondsL != null){
                selectedItemSecondsL!!.setBackgroundResource(R.color.ListViewItemsColor)
            }
        }
    }

    private fun AdjustLayoutSize()
    {
        //For resolution
        val display = windowManager.defaultDisplay
        val size = Point()
        display.getSize(size)
        //Get DPI
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        //Initialize LinearLayout for changing TimePicker Dialog Size;
        linearLayout = findViewById(R.id.TimePickerLayout) as LinearLayout
        val layoutParams = linearLayout!!.layoutParams as ViewGroup.MarginLayoutParams
        //Nexus 5X device 420dpi 1080x1920 == 1794
        if(size.y >= 1794 && size.y < 1920){
            layoutParams.topMargin = 656
            layoutParams.rightMargin = 80
            layoutParams.leftMargin = 80
        }
        //Redmi note 3 DPI 480 1080x1920
        else if(size.y == 1920){
            layoutParams.topMargin = 630
            layoutParams.rightMargin = 80
            layoutParams.leftMargin = 80
        }
        //Pixel XL device 480dpi 1440x2560 == 2416
        else if(size.y > 1920 && displayMetrics.densityDpi >= 480){
            layoutParams.topMargin = 1098
            layoutParams.rightMargin = 125
            layoutParams.leftMargin = 125
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
        overridePendingTransition(R.anim.top_to_bottom1,R.anim.top_to_bottom2)
    }
}