关于DialogFragment对话框

dialogFragment

这个是对话框碎片,官方推荐使用
如果在一个活动之中,进行dialogFragment和Fragment传递参数的话 需要设置 setsetTargetFragment(this)参数才可以接受dialogfragment传递过来到值,并且fragmen中的onActivityResult方法才能接受得到。
具体方式看实例:
/**

  • 时间选择器 日期+时间选择 工具
    */
    open class TimeDialog :DialogFragment(),DatePickerDialog.OnDateSetListener,TimePickerDialog.OnTimeSetListener {

    val c = Calendar.getInstance()
    var hour = c.get(Calendar.HOUR_OF_DAY)
    var minute = c.get(Calendar.MINUTE)
    var year = c.get(Calendar.YEAR) // 年
    var month = c.get(Calendar.MONTH) //月
    var day = c.get(Calendar.DAY_OF_MONTH) //日
    val intent = Intent()

override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
intent.putExtra(“year”,year.toString())
intent.putExtra(“month”,(month+1).toString())
intent.putExtra(“day”,dayOfMonth.toString())
TimePickerDialog(activity,this, hour, minute,true).show()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return DatePickerDialog(activity,this,year,month,day)
}
override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
intent.putExtra(“hour”,hourOfDay.toString())
intent.putExtra(“minute”,minute.toString())
targetFragment?.onActivityResult(DATETIME,Activity.RESULT_OK,intent)
println(“回调方法1”)
}
}

你可能感兴趣的:(小知识)