gpt4 book ai didi

android - 如何在时间选择器中将文本 “cancel”更改为android kotlin中的另一个文本?

转载 作者:行者123 更新时间:2023-12-02 13:33:40 27 4
gpt4 key购买 nike

  class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c: Calendar = Calendar.getInstance()
val year: Int = c.get(Calendar.YEAR)
val month: Int = c.get(Calendar.MONTH)
val day: Int = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(getActivity()!!, this, year, month, day)
}

override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
val tv: TextView = activity!!.findViewById(R.id.txt_date_picker) as TextView
tv.setText("$day - $month - $year")
}

override fun onCancel(dialog: DialogInterface?) {
Toast.makeText(activity,"date picker Canceled.", Toast.LENGTH_SHORT).show()
super.onCancel(dialog)

}
}

编辑:尝试答案后的所有代码:
class TimePickerFragment: DialogFragment(), TimePickerDialog.OnTimeSetListener{
private lateinit var calendar:Calendar

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
calendar = Calendar.getInstance()


val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)


return TimePickerDialog(
activity, // Context
android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth, // Theme
this, // TimePickerDialog.OnTimeSetListener
hour, // Hour of day
minute, // Minute
false // Is 24 hour view
)//.apply {
// getButton(DialogInterface.BUTTON_NEGATIVE).setText(R.string.myCancelString)
// }


}


override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
// Do something with the returned time
val tv:TextView = activity.findViewById(R.id.end) as TextView
// tv.text = "${getHourAMPM(hourOfDay)}:$minute ${getAMPM(hourOfDay)}"
tv.text = "$hourOfDay:$minute "
}

}

最佳答案

创建对话框后,您可以获取按钮并更改其文本,这发生在onStart()中:

override fun onStart() {
super.onStart()
(dialog as? DatePickerDialog)
?.getButton(DialogInterface.BUTTON_NEGATIVE)
?.setText(R.string.myCancelString)
}

关于android - 如何在时间选择器中将文本 “cancel”更改为android kotlin中的另一个文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60097173/

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