在我的活动中,我有两个TextView(startDatePicker和endDatePicker),当单击它们时,应该显示一个datePicker对话框。到这里为止一切正常,但是在用户选择日期之后,我需要用用户的选择来设置相应单击的textView的文本。 我发现了另一条这样的帖子,并从中得到了提示:https://stackoverflow.com/a/24534147/10113273
这是我的DatePickerDialogFragment类:
public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public static final int START_DATE = 0;
public static final int END_DATE = 1;
private int current = 0;
private int chosenDate;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
Bundle bundle = this.getArguments();
if (bundle != null) {
chosenDate = bundle.getInt("DATE", 0);
}
switch (chosenDate) {
case START_DATE:
current = START_DATE;
return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
case END_DATE:
current = END_DATE;
return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
}
return null;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (current == START_DATE) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
//Set the startDate textView text with the dateString
} else {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
//Set the endDate textView text with the dateString
}
}
}
在onDateSet内,如何访问textView?我可能认为答案是无法完成的。如果我是对的,该怎么办才能解决这个问题?
在此处在“活动”的textview上设置onClickListener的位置:
startDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("DATE",0);
DialogFragment dialogFragment = new DatePickerDialogFragment();
dialogFragment.setArguments(bundle);
dialogFragment.show(getSupportFragmentManager(),"date picker");
}
});
endDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("DATE",1);
DialogFragment dialogFragment = new DatePickerDialogFragment();
dialogFragment.setArguments(bundle);
dialogFragment.show(getSupportFragmentManager(),"date picker");
//endDatePicker.setText(endDate);
}
});
答案 0 :(得分:0)
如果不使用DatePickerDialogFragment
实现侦听器,可能会更好(我知道它在Android examples中,但是您已经注意到这是一种不灵活的设计)。如果您像这样向setListener
添加DatePickerDialogFragment
方法:
public class DatePickerDialogFragment extends DialogFragment {
private DatePickerDialog.OnDateSetListener listener = null;
void setListener(DatePickerDialog.OnDateSetListener listener) {
this.listener = listener;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), listener, year, month, dayOfMonth);
}
}
然后可以轻松地与不同的日期选择器一起使用,易于在整个应用中重用,并且可以将结果直接放入启动选择器的活动中的其他视图中,例如(下面的示例代码将包含在您的活动中onCreate
方法)
final DatePickerDialog.OnDateSetListener startListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
// set the start date TextView
startDatePicker.setText(dateString);
}
};
final DatePickerDialog.OnDateSetListener endListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String dateString = DateFormat.getDateInstance().format(calendar.getTime());
// set the enddate TextView
endDatePicker.setText(dateString);
}
};
// NOTE: Use getSupportFragmentManager or getFragmentManager below
// depending on which type of DialogFragment you extended
// Reset the listener if the screen was rotated
if (savedInstanceState != null) {
DatePickerDialogFragment dpf;
dpf = (DatePickerDialogFragment) getSupportFragmentManager().findFragmentByTag("myStartDatePicker");
if (dpf != null) {
dpf.setListener(startListener);
}
dpf = (DatePickerDialogFragment) getSupportFragmentManager().findFragmentByTag("myEndDatePicker");
if (dpf != null) {
dpf.setListener(endListener);
}
}
startDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialogFragment dpf = new DatePickerDialogFragment();
// set bundle args on dpf if still needed
dpf.setListener(startListener);
dpf.show(getSupportFragmentManager(), "myStartDatePicker");
}
});
endDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialogFragment dpf = new DatePickerDialogFragment();
// set bundle args on dpf if still needed
dpf.setListener(endListener);
dpf.show(getSupportFragmentManager(), "myEndDatePicker");
}
});