使用单个侦听器多次调用DatePicker

时间:2015-03-05 05:10:53

标签: android datepicker

我有相同形式的日期和时间字段。所以我需要为这些领域选择四个日期和时间。目前我能够得到它,但是当我从第一个选择器中选择时间和日期时,所有字段都显示相同的时间和日期,并且所有字段都已设置。

我使用了几种方法,一些是检查每个字段的视图ID,给对话框提供id并在onCreateDialog方法中使用switch等等。但是没有什么对我有用。请建议我如何实现这一目标。

我的DatePicker代码:

 public void showDatePicker() {
        DatePickerFragment date1 = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date1.setArguments(args);

    /**
     * Set Call back to capture selected date
     */
    date1.setCallBack(ondate);
    date1.show(getSupportFragmentManager(), "Date Picker");



}




    DatePickerDialog.OnDateSetListener ondate =  new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {




            date.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
                    + "-" + String.valueOf(year));

            dateValue = date.getText().toString();

            est_compDate.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
                    + "-" + String.valueOf(year));

            estValue = est_compDate.getText().toString();


            act_compDate.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
                    + "-" + String.valueOf(year));

            actValue = act_compDate.getText().toString();



            notify_date.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
                    + "-" + String.valueOf(year));

            notifyValue = notify_date.getText().toString();



    }
};

我的DatePickerFragment代码:

public class DatePickerFragment extends DialogFragment {
    DatePickerDialog.OnDateSetListener ondateSet;

public DatePickerFragment() {
}
public void setCallBack(DatePickerDialog.OnDateSetListener ondate) {

    ondateSet = ondate;

}



private int year, month, day;


@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    year = args.getInt("year");
    month = args.getInt("month");
    day = args.getInt("day");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


    return new DatePickerDialog(getActivity(), ondateSet, year, month, day);


}

}

因此,当我为date.setText行选择时间和日期选择器时,所有其他三个都在一次设置。

我使用onClick方法调用picker方法:

public void datePicker(View view){

    showDatePicker();

}

请让我知道可以做些什么。如果您需要更多信息,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:3)

更新: 在DialogFragment中创建一个接口,并在其中放入一个抽象方法,该方法将在参数中以字符串形式保存日期。然后在您的主活动中实现此接口,并在您的活动中覆盖DateInString(String)方法,该方法将为您提供活动中的选定日期。然后您可以将其显示在您想要的位置。 我正在向您展示一个工作代码,我必须在两个地方设置日期,您可以通过修改活动中的DateInString(String)覆盖方法多次使用它...

DialogFragment:

public class Date extends android.support.v4.app.DialogFragment implements DatePickerDialog.OnDateSetListener {

    DatePickerData datePickerData ;
    String date = null;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        byte month = (byte)cal.get(Calendar.MONTH);
        byte day = (byte ) cal.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(),this,year,month,day);
    }


    @Override
    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDay) {
    Calendar cal = Calendar.getInstance();
        cal.set(selectedYear,selectedMonth,selectedDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
      date = dateFormat.format(cal.getTime());
        passDate(date);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        datePickerData = (DatePickerData)activity;
    }

    public void passDate(String date){
    datePickerData.DateInString(date);   
    public interface DatePickerData{
        public void DateInString(String date);
    }
}

和你的活动:

public class ActMarkAttendance extends FragmentActivity implements Date.DatePickerData{
private boolean isStartDate = false;
private TextView startDateTextView;
    private TextView endDateTextView;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_view_attendance);
        startDateTextView = (TextView) findViewById(R.id.startDateTextView_attView);
        endDateTextView = (TextView) findViewById(R.id.endDateTextView_attView);
startDateTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            isStartDate = true;                
            showDateFragment();                  
            }
        });

        endDateTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isStartDate = false;
               showDateFragment();                    
            }
        });
}
private void showDateFragment(){
        Date dateFragment = new Date();
        dateFragment.show(fm,General.NOTHING);
    }
@Override
    public void DateInString(String date) {
    if (isStartDate)
        startDateViewer.setText(date);
    else
        endDateViewer.setText(date);

    }
}