Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

kotlin - How to write koltin class for date picker and timepicker that call on at any time?

Here is the code from the class that I am using the datepicker. I can't get where to code the call for the method.

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Build
import android.widget.DatePicker
import android.widget.TimePicker
import androidx.annotation.RequiresApi
import androidx.fragment.app.DialogFragment
import java.util.*


class SetApp : DialogFragment(), DatePickerDialog.OnDateSetListener,    TimePickerDialog.OnTimeSetListener {

    var month = 0
    var day = 0
    var year = 0
    var hours = 0
    var min = 0

    var savedMonth = 0
    var savedDay = 0
    var savedYear = 0
    var savedHours = 0
    var savedMin = 0

     fun getDateTimeCalendar(){
        val cal :Calendar = Calendar.getInstance()
        day = cal.get(Calendar.DAY_OF_MONTH)
        month = cal.get(Calendar.MONTH)
        year = cal.get(Calendar.YEAR)
        hours = cal.get(Calendar.HOUR)
        min = cal.get(Calendar.MINUTE)
    }
//This the first method that I have tried to call but the program goes back to the main activity. 

    fun pickDate() {
        getDateTimeCalendar()
        DatePickerDialog(this, this, year, month, day).show()
    }

     fun DatePickerDialog(setApp: SetApp, setApp1: SetApp, year: Int, month: Int, day: Int) {

    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        TODO("Not yet implemented")
        savedDay = dayOfMonth
        savedMonth = month
        savedYear = year

        getDateTimeCalendar()
        TimePickerDialog(this, this, hours, min, true)
    }

    private fun TimePickerDialog(unit: SetApp, unit1: SetApp, hours: Int, min: Int, unit2: Boolean) {

    }

    override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
        TODO("Not yet implemented")
        savedHours = hourOfDay
        savedMin = minute
    }

This is the second method that I have tried to do but it does the same thing happen

    @RequiresApi(Build.VERSION_CODES.N)
    fun clickDataPicker() {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)


        val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            // Display Selected date in Toast

        } as SetApp, year, month, day)
        dpd.show()
    }
}

private fun Unit.show() {
    TODO("Not yet implemented")
}

This is how I am calling the method and class from one of these activities. The idea is that I can call that this method when I need it. This is for a scheduled app.

package com.example.scheduleit

import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity

class NewPlan: AppCompatActivity(){

    @RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.newplan)

    val thedate = findViewById<Button>(R.id.btnDate)
    val setApp = SetApp()


    thedate.setOnClickListener {
        setApp.clickDataPicker()
    }
}

}

question from:https://stackoverflow.com/questions/65926924/how-to-write-koltin-class-for-date-picker-and-timepicker-that-call-on-at-any-tim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...