gpt4 book ai didi

Android Kotlin DialogFragment (TimePicker)

转载 作者:太空狗 更新时间:2023-10-29 15:44:24 26 4
gpt4 key购买 nike

我在使用 Kotlin 中的 Android 显示 DialogFragment 时遇到问题。这是我到目前为止得到的:

TimePickerFragment.kt

package de.flokol120.UniversalSleepTimer

import android.app.Dialog
import android.app.DialogFragment
import android.widget.TimePicker
import android.app.TimePickerDialog
import android.os.Bundle
import android.text.format.DateFormat
import java.util.*


/**
* @author flokol120
* TimePicker Fragment to get the time
*/
class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
val c = Calendar.getInstance()
val hour = c.get(Calendar.HOUR_OF_DAY)
val minute = c.get(Calendar.MINUTE)

return TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()))
}

override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
//TODO add this
}
}

AddTimer.kt

package de.flokol120.UniversalSleepTimer

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button


/**
* @author flokol120
* the fragment which adds a simple kill Timer
*/
class AddTimer : Fragment() {

lateinit var timePicker : Button

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v = inflater!!.inflate(R.layout.fragment_add_timer, container, false)

timePicker = v.findViewById(R.id.timePicker)

timePicker.setOnClickListener {
val newFragment = TimePickerFragment()
newFragment.show(fragmentManager, "timePicker")
}

return v
}

}

我也尝试用 this@AddTimer.fragmentManager 替换 fragmentManager 但这似乎不是解决方案。

我现在很无能,因为 Android Studio 说我必须使用 FragmentManager! 作为第一个参数,但我正在使用一个,对吗?

最佳答案

Aliaksandr Kavalenka想出了一个巧妙的解决方法,用于日期选择器。我将他的想法格式化为与时间选择器一起工作:

fun getTime(textView: TextView, context: Context){

val cal = Calendar.getInstance()

val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
cal.set(Calendar.HOUR_OF_DAY, hour)
cal.set(Calendar.MINUTE, minute)

textView.text = SimpleDateFormat("HH:mm").format(cal.time)
}

textView.setOnClickListener {
TimePickerDialog(context, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true).show()
}
}

关于Android Kotlin DialogFragment (TimePicker),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com