在我的应用程序中,我必须显示4-5个日期选择器并使用datepicker对话框片段设置日期如何执行此操作?
目前我必须为所有的datepicker对话框制作单独的日期选择器......
public static class RegistrationDatePicker 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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mTextView.setText(String.valueOf(month + "/" + day + "/" + year));
}
}
答案 0 :(得分:3)
由于我最近不得不在同一个活动中使用多个DatePicker DialogFragment,我想分享我的答案。
首先是我的DatePicker DialogFragment,用接口实现:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener{
// This is the listener for the activity
private DatePickerFragmentListener listener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the current date to start the DatePicker with
final Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
listener.onDateSet(year, month, dayOfMonth);
}
// This is the method from the DatePicker fragment to implement in the Main Activity
public interface DatePickerFragmentListener{
public void onDateSet(int year, int month, int day);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (DatePickerFragmentListener) context;
}
}
现在实现DatePickerFragmentListener的MainActivity:
public class MainActivity extends AppCompatActivity
implements DatePickerFragment.DatePickerFragmentListener{
Button myButton1, myButton2;
FragmentManager fm = getSupportFragmentManager();
int DATE_DIALOG = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton1 = (Button) findViewById(R.id.myButton2);
myButton2 = (Button) findViewById(R.id.myButton3);
// SELECT A START DATE
myButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DATE_DIALOG = 1;
openDialog();
}
});
// SELECT A STOP DATE
myButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DATE_DIALOG = 2;
openDialog();
}
});
}
// I always show the same DatePicker fragment
public void openDialog(){
DatePickerFragment datepickDialog = new DatePickerFragment();
datepickDialog.show(fm, "Start Date");
}
@Override
public void onDateSet(int year, int month, int day) {
//Depending on the DATE_DIALOG key, I update the text of the corresponding button
if(DATE_DIALOG ==1){
myButton2.setText( Integer.toString(day) + "/" +
Integer.toString(month+1)+ "/" +
Integer.toString(year));
}
else if(DATE_DIALOG ==2){
myButton2.setText( Integer.toString(day) + "/" +
Integer.toString(month+1)+ "/" +
Integer.toString(year));
}
}
}
答案 1 :(得分:0)
是的,您可以在界面的帮助下将单个DialogFragment用于多个DatePicker 您必须在您的自定义DialogFragment中创建接口 RegistrationDatePicker ,如下所示:
public void OnDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
switch (view.Id)
{
case Resource.Id.datepicker_one:
Log.Debug("SO", $"DatePicker One Value: {view.DateTime}");
break;
case Resource.Id.datepicker_two:
Log.Debug("SO", $"DatePicker Two Value: {view.DateTime}");
break;
}
}
public class CustomDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public interface InterfaceCommunicator {
void sendRequestCode(int Code);
}
String activityCode;
Button btn;
public CustomDialog(){
super();
}
@SuppressLint("ValidFragment")
public CustomDialog(String activityCode, Button btn){
super();
this.activityCode = activityCode;
this.btn = btn;
}
InterfaceCommunicator interfaceCommunicator;
@NonNull
@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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
btn.setText(String.valueOf(month + "/" + day + "/" + year));
interfaceCommunicator.sendRequestCode(1);
}
@Override
public void onAttach(Activity activity) {
interfaceCommunicator = (InterfaceCommunicator) activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
interfaceCommunicator = null;
super.onDetach();
}
}
希望这可以帮到你