android - View 在自定义对话框 Android Kotlin 中为空
问题描述
我在 kotlin 类中创建了自定义对话框。
这是我的代码:
class SPCalendarDialog(context: Context) : Dialog (context,R.style.DialogTheme) {
private var listener: OnSPCalendarListener? = null
private var year: Int = 0
private var month: Int = 0
private var dayOfMonth: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setCancelable(true)
setCanceledOnTouchOutside(true)
setContentView(R.layout.sp_calendar_dialog)
val calendar = Calendar.getInstance()
year = calendar.get(Calendar.YEAR)
month = calendar.get(Calendar.MONTH)
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
calendarView.descendantFocusability = DatePicker.FOCUS_BLOCK_DESCENDANTS
calendarView.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)) { _, y, m, dM ->
year = y
month = m
dayOfMonth = dM
}
}
fun setDate(date: Long) {
val calendar = Calendar.getInstance()
if (date != 0L)
calendar.timeInMillis = date
calendarView.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)) { _, y, m, dM ->
year = y
month = m
dayOfMonth = dM
}
}
interface OnSPCalendarListener {
fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int)
}
fun setListener(listener: OnSPCalendarListener) {
this.listener = listener
}
}
我这样称呼这个类。
private fun showSPCalendarDialog(view: View) {
var spCalendarDialog = SPCalendarDialog(view.context)
var birthDate: Long = 0
spCalendarDialog.setListener(object : SPCalendarDialog.OnSPCalendarListener {
override fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int) {
}
})
spCalendarDialog.setDate(birthDate)
spCalendarDialog.show()
}
当我尝试运行我的应用程序并显示我的自定义对话框时,在 setDate 方法中,calendarView 为空。这是我的 xml 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dimen_p_30"
android:layout_marginRight="@dimen/dimen_p_30"
app:cardBackgroundColor="@color/white"
android:layout_centerInParent="true"
app:cardCornerRadius="@dimen/dimen_p_15">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<DatePicker
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:descendantFocusability="blocksDescendants" />
<RelativeLayout
android:id="@+id/dialog_ok_button"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_p_46"
android:background="@color/light_blue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="@font/myriad_geo_mtavruli_bold"
android:text="@string/approve"
android:textColor="@color/white"
android:textSize="@dimen/dimen_p_14" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
我正在使用 kotlinx 从没有 findById 的 xml 中查找视图
import kotlinx.android.synthetic.main.sp_calendar_dialog.*
谁能告诉我,我做错了什么?ps 我是 Kotlin 的初学者。谢谢
解决方案
我猜在调用的时候setDate
calendarView
还没有初始化。显示对话框后尝试调用setDate
一段时间:
private fun showSPCalendarDialog(view: View) {
var spCalendarDialog = SPCalendarDialog(view.context)
var birthDate: Long = 0
spCalendarDialog.setListener(object : SPCalendarDialog.OnSPCalendarListener {
override fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int) {
}
})
spCalendarDialog.show()
view.postDelayed({ spCalendarDialog.setDate(birthDate) }, 500)
}
推荐阅读
- git - 阿特拉斯彩虹
- node.js - expressjs、nginx 代理、csurf 和安全 cookie 不起作用
- javascript - 使用单选按钮填充复选框列表
- python - Pandas 组合数据帧,删除所有初始数据帧中未出现值的行
- oracle - 使用过程创建物化视图
- python-3.x - 如何在python中将大型二进制文件转换为pickle字典?
- c++ - 使用 SetupDiGetDeviceRegistryPropertyW 获取所需大小的错误代码 122
- android - xml中的Android可绘制形状
- javascript - 为什么可以在 JSON 中定义函数?
- python - 登录时向用户显示烧瓶中的处理结果