java - 将按钮的文本设置为 datepicker 上的选定日期
问题描述
我正在尝试将按钮的文本更改为所选日期以进一步存储在 firebase 数据库中。这是我使用的java代码:
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new MortgageProtectionActivity.DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
按钮的 ID 是 btn_pf_dob 和 onclick 的 showDatePickerDialog。
解决方案
您可以覆盖该onDateSet
方法以在按钮或其他地方放置一个值。要使用对话框片段执行此操作,您可以实现主要活动OnDateSetListener
而不是对话框(请参阅此问题或此问题)。或者,请参阅下面的编辑 2。
编辑1:如果您不需要使用 a DialogFragment
,另一种方法是
public void showDatePickerDialog(View v) {
final Button btn = findViewById(R.id.btn_pf_dob);
final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// generate string from year, month, and day
String dateStr = yourMethodHere(year,month,day);
btn.setText(dateStr);
}
};
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this, dateSetListener, year, month, day);
dpd.show();
}
编辑 2:如果你需要使用 aDialogFragment
但不想让你的 Activity 实现onDateSet
,你可以使用类似下面的东西。你为什么要这个?如果您有多个从同一个活动启动的日期选择器,您可能希望每个活动都有不同的侦听器。在 Activity 上实现它可以防止这种情况发生,而这种方法可以让您为每个日期选择器创建一个唯一的侦听器。
public static class DatePickerFragment extends DialogFragment {
private DatePickerDialog.OnDateSetListener listener = null;
void setListener(DatePickerDialog.OnDateSetListenerlistener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dpd = null;
if( listener != null ) {
// Use the current date as the default date in the picker
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog
dpd = new DatePickerDialog(getActivity(), listener, year, month, day);
}
return dpd;
}
}
public void showDatePickerDialog(View v) {
final Button btn = findViewById(R.id.btn_pf_dob);
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
String dateStr = "date: " + year + " " + month + " " + day;
btn.setText(dateStr);
}
}
);
newFragment.show(getFragmentManager(), "datePicker");
}
要在 Activity 中正确设置它,使用屏幕旋转,您需要在 Activity 中重置监听器,onCreate
以便在显示对话框时旋转屏幕,例如
if (savedInstanceState != null) {
DatePickerDialogFragment dpf = (DatePickerDialogFragment) getFragmentManager()
.findFragmentByTag("datePicker");
if (dpf != null) {
dpf.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
String dateStr = "date: " + year + " " + month + " " + day;
btn.setText(dateStr);
}
}
);
}
}
推荐阅读
- python - 如何在 Python3 中测试对象是否具有 odict_values 类型?
- javascript - 在进行修改时需要一个“for-of”循环而不是一个“for”循环
- c++11 - 如何使用模板获取位域大小?
- jquery - 如何以角度实现Jquery multislider
- python - NOT NULL 约束失败:users_user.email
- scala - 遍历数据框并获取索引
- macos - 无法将屏幕截图保存在指定路径或将屏幕截图附加到 MAC 中的 extentreports
- spring - 将 OAuth2AuthorizedClient 解析为 Spring bean
- .htaccess - 如何在 yii2 和 htaccess 中使我的 url 看起来像 www.example.com/userid
- javascript - 测试功能组件内点击事件的功能/方法调用