如何为多个Date控件制作generalize dateFragment类?

时间:2014-01-15 10:17:56

标签: android android-dialogfragment android-datepicker

我想对多个控件进行泛化/公共类调用,例如From&到目前为止对话框 调用时应使用相同的类

这里我有两个标签lblSrchFrmDate& lblSrchToDate想要显示两者的日期对话框

我是否需要为每次通话创建DatePickerFragment?

这是我的代码 DatePickerFragment.java 类将有

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

String formattedDt = null;
public Dialog onCreateDialog(View v) {
    // 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 selectedYear, int selectedMonth, int selectedDay) {              
    formattedDt = DateUtil.setSelectedDateInSDF(selectedYear, selectedMonth, selectedDay);              
    //lblSrchFrmDate .setText(formattedDt);<--- want to set selected date on label 
}

}

onClick代码

// On click Of Label
@Override
public void onClick(View v) {

    switch (v.getId()) {        
    case R.id.lblSrchFrmDate:
        showDatePickerDialog(v);
        break;
    case R.id.lblSrchToDate:            
        showDatePickerDialog(v);
        break;
    }
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

如何使用View v为各个对话框呼叫控件设置日期? v.getId()给我数字id,它显示我的值id / datePicker,它不是标签id

0 个答案:

没有答案