android - Android Studio 如何使用 Fragment 在 Kotlin 中打开日期选择器
问题描述
我尝试了一切,但似乎无法让应用程序打开日期选择器对话框。应用程序每次都崩溃。我尝试了多种解决方案,但似乎都有相同的结果。如何在片段中打开 Kotlin 中的日期选择器?
该代码在我在 Activity 类中使用时有效,但我希望它在 Fragment 中工作。
calendar = Calendar.getInstance()
year = calendar.get(Calendar.YEAR)
month = calendar.get(Calendar.MONTH)
day = calendar.get(Calendar.DAY_OF_MONTH)
showDate(year, month + 1, day)
fun setDate(view: View?) {
showDialog(999)
Toast.makeText(
applicationContext, "Please select a date",
Toast.LENGTH_SHORT
)
.show()
}
override fun onCreateDialog(id: Int): Dialog {
return if (id == 999) {
DatePickerDialog(
this,
myDateListener, year, month, day
)
} else onCreateDialog(1)
}
private val myDateListener =
DatePickerDialog.OnDateSetListener { arg0, arg1, arg2, arg3 ->
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2 + 1, arg3)
}
/**
* This method shows the selected date in the view
*/
private fun showDate(year: Int, month: Int, day: Int) {
dateView.text = StringBuilder().append(day).append("/")
.append(month).append("/").append(year)
}
showDialog() 在 Fragment 中不起作用,我尝试实现 DialogFragment 但无济于事。我该如何解决这个问题?
解决方案
我认为不需要DialogFragment。尝试:
创建单独MyDatePickerDialog
的类
import android.app.DatePickerDialog
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentActivity
import java.util.*
class MyDatePickerDialog(
// context needed to create the dialog
activity: FragmentActivity,
// callback for the caller of this dialog
onShowDateClicked: (Int, Int, Int) -> Unit
) {
private val calendar = Calendar.getInstance()
val year: Int
get() = calendar.get(Calendar.YEAR)
val month: Int
get() = calendar.get(Calendar.MONTH)
val day: Int
get() = calendar.get(Calendar.DAY_OF_MONTH)
private val dialog = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
// on API 23 or lower, use the older constructor that requires initial date
DatePickerDialog(
activity,
{ _, year, month, day -> onShowDateClicked.invoke(year, month, day) },
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
)
} else {
DatePickerDialog(activity).apply {
setOnDateSetListener { _, year, month, day ->
onShowDateClicked.invoke(year, month, day)
}
}
}
fun show() {
dialog.show()
}
}
假设MainFragment
是您要显示对话框的位置,请执行以下操作:
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// here, put correct id of the view which click event is suppose to open the dialog
view_to_open_dialog?.setOnClickListener {
activity?.takeIf { !it.isFinishing && !it.isDestroyed }?.let { activity ->
MyDatePickerDialog(activity, ::showDate).show()
}
}
}
private fun showDate(year: Int, month: Int, day: Int) {
Log.d("Picked Date", "$year-$month-$day")
// the rest of your logic here
}
}
推荐阅读
- javascript - ES6:防止函数摇晃
- mongodb - MongoDB 中的元素列表后缺少“]”
- ssl - git get 错误:“无法识别对等证书颁发者”
- python - KeyError:“json 文件不再支持将列表喜欢传递给 .loc 或 [] 并带有任何缺失的标签
- transactions - 如何从 Quickbooks 获取销售订单和库存交易?
- ios - DeviceMotionEvent.requestPermission() 提示文本
- microservices - 微服务平台的 Keycloak 客户端 ID 策略
- shell - 使用脚本创建 N 个文件夹
- android - Expo Client 未在 Mac 上安装?
- php - Laravel Webrtc 视频聊天与 react 和 pusher