单击一次 EditText 后显示 DatePickerDialog 和 TimePickerDialog
Show DatePickerDialog and TimePickerDialog after one time EditText click
我在 AndroidViewModel 上单击 EditText 后尝试显示 DataPickerDialog 和 TimePickerDialog。
// fragment
// in TestFragment
// pass the `requireActivity().application` to TextViewModel
// viewModel
class TestViewModel(
application: Application,
) : AndroidViewModel(application) {
...
fun showDateTimeDialog() {
val currentDateTime = Calendar.getInstance()
val startYear = currentDateTime.get(Calendar.YEAR)
val startMonth = currentDateTime.get(Calendar.MONTH)
val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
val startMinute = currentDateTime.get(Calendar.MINUTE)
DatePickerDialog(getApplication(), { _, year, month, day ->
TimePickerDialog(getApplication(), { _, hour, minute ->
val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute)
}, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show()
}
}
xml(数据绑定)
<EditText
android:id="@+id/date_time_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/add_date_time"
android:importantForAutofill="no"
android:ems="10"
android:clickable="true"
android:focusable="false"
android:editable="false"
android:onClick="@{() -> viewModel.showDateTimeDialog()}" />
但是当我点击这个 EditText 时,它报错显示 activity 在显示这两个对话框时消失了。
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
原因是上下文。
对话框的上下文必须是 activity,而不是应用程序。
将应用程序上下文设置为对话框将导致 WindowManger$BadTokenException。
import android.app.Application
import androidx.lifecycle.AndroidViewModel
class TestViewModel(application: Application) : AndroidViewModel(application) {
private val context = getApplication<Application>().applicationContext
........
}
然后传递上下文
fun showDateTimeDialog() {
val currentDateTime = Calendar.getInstance()
val startYear = currentDateTime.get(Calendar.YEAR)
val startMonth = currentDateTime.get(Calendar.MONTH)
val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
val startMinute = currentDateTime.get(Calendar.MINUTE)
DatePickerDialog(context, { _, year, month, day ->
TimePickerDialog(context, { _, hour, minute ->
val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute)
}, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show()
}
}
我在 AndroidViewModel 上单击 EditText 后尝试显示 DataPickerDialog 和 TimePickerDialog。
// fragment
// in TestFragment
// pass the `requireActivity().application` to TextViewModel
// viewModel
class TestViewModel(
application: Application,
) : AndroidViewModel(application) {
...
fun showDateTimeDialog() {
val currentDateTime = Calendar.getInstance()
val startYear = currentDateTime.get(Calendar.YEAR)
val startMonth = currentDateTime.get(Calendar.MONTH)
val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
val startMinute = currentDateTime.get(Calendar.MINUTE)
DatePickerDialog(getApplication(), { _, year, month, day ->
TimePickerDialog(getApplication(), { _, hour, minute ->
val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute)
}, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show()
}
}
xml(数据绑定)
<EditText
android:id="@+id/date_time_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/add_date_time"
android:importantForAutofill="no"
android:ems="10"
android:clickable="true"
android:focusable="false"
android:editable="false"
android:onClick="@{() -> viewModel.showDateTimeDialog()}" />
但是当我点击这个 EditText 时,它报错显示 activity 在显示这两个对话框时消失了。
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
原因是上下文。 对话框的上下文必须是 activity,而不是应用程序。 将应用程序上下文设置为对话框将导致 WindowManger$BadTokenException。
import android.app.Application
import androidx.lifecycle.AndroidViewModel
class TestViewModel(application: Application) : AndroidViewModel(application) {
private val context = getApplication<Application>().applicationContext
........
}
然后传递上下文
fun showDateTimeDialog() {
val currentDateTime = Calendar.getInstance()
val startYear = currentDateTime.get(Calendar.YEAR)
val startMonth = currentDateTime.get(Calendar.MONTH)
val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
val startMinute = currentDateTime.get(Calendar.MINUTE)
DatePickerDialog(context, { _, year, month, day ->
TimePickerDialog(context, { _, hour, minute ->
val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute)
}, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show()
}
}