如何在其他类中实现DatePicker Fragment类

时间:2014-12-10 05:59:05

标签: android

我有一个像这样定义的DatePickerFragment类。如何在另一个类中实现它,而不是完全再次定义它。请指导。

public class DatePickerFragment extends DialogFragment {
    OnDateSetListener ondateSet;

    public DatePickerFragment() {
    }

    public void setCallBack(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);
    }
   }  

如何在以下代码中使用此类?请指导。

public class TrainingDetails extends BaseFragment
{
    View view;
    private EditText nameEdt;
    private EditText descriptionEdt;
    private Spinner trainingTypes;
    private Spinner trainerProfile;

    private NumberPicker plannedBudgetNp;
    private Calendar calendar;
    private EditText startDt;
    private EditText endDt;
    private int year, month, day;

    String data1[] = {"1", "2", "3"};
    String data2[] = {"a", "b", "c"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.training_details, container,false);

        nameEdt = (EditText) view.findViewById(R.id.name_edt);

        trainingTypes = (Spinner) view.findViewById(R.id.trainingtypes_spn);

        ArrayAdapter<String> adapter_state1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);

        adapter_state1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        trainingTypes.setAdapter(adapter_state1);

        trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                int index = arg0.getSelectedItemPosition();
                String selTrainingTypes = data1[index];
                //Toast.makeText(arg0.getContext(), "Selected: " + selectedItem, Toast.LENGTH_LONG).show();

            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {

            }
        });

        trainerProfile = (Spinner) view.findViewById(R.id.trainerprofile_spn);

        ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data2);

        adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        trainingTypes.setAdapter(adapter_state2);

        trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                int index = arg0.getSelectedItemPosition();
                String selTrainerProfiles = data2[index];
                //Toast.makeText(arg0.getContext(), "Selected: " + selectedItem, Toast.LENGTH_LONG).show();

            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {

            }
        });

        descriptionEdt = (EditText) view.findViewById(R.id.description_edt);

        startDt = (EditText) view.findViewById(R.id.startdate_edt);
        endDt = (EditText) view.findViewById(R.id.enddate_edt); 

        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        showDate(year, month+1, day);

1 个答案:

答案 0 :(得分:0)

DatePickerFragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    int currentDate,currentMonth , currentYear;
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        currentDate = calendar.get(Calendar.DAY_OF_MONTH);
        currentMonth = calendar.get(Calendar.MONTH);
        currentYear = calendar.get(Calendar.YEAR);
        return new DatePickerDialog(getActivity(), this, currentYear    , currentMonth, currentDate);
    }
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        String finalDate=String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear)+"/"+String.valueOf(year));
    }

}

调用SecondActivity

DatePickerFragment newFragment = new DatePickerFragment();            newFragment.show(getActivity()。getSupportFragmentManager(),“datePicker”);