日期选择对话框DatePickerFragment
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements
OnDateSetListener {
static int syear = 1970;
static int smonth = 0;
static int sday = 0;
TheListener listener;
public interface MyDateListener {
public void returnDate(String date);
}
public DatePickerFragment(MyDateListener listener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO 日期选择完成事件
syear = year;
smonth = monthOfYear + 1;
sday = dayOfMonth;
if (null != listener) {
Log.d("xwj", "year=" + year + ",monthOfYear=" + monthOfYear
+ ",dayOfMonth=" + dayOfMonth);
listener.returnDate(syear + "-" + smonth + "-" + sday);
}
}
}
Activity中调用DialogFragment fragment = new DatePickerFragment(myDateListener);
fragment.show(getSupportFragmentManager(), "datePicker");
弹出日期选择对话框,
日期选择对话框中的日期通过自定义接口传递给 需要的Activity
// 显示时间选择器
private void showDatePickerFragemnt() {
DatePickerFragment.MyDateListener myDateListener = new MyDateListener() {
public void returnDate(String date) {
route_stime.setText(date);
}
};
DialogFragment fragment = new DatePickerFragment(myDateListener);
fragment.show(getSupportFragmentManager(), "datePicker");
}