如何从Android的DatePicker小部件获取date?
我在Android中使用DatePicker
小部件来为用户设置一个date,并且想要在点击确认button时获取date值,我该怎么做?
尝试这个:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1); int day = datePicker.getDayOfMonth(); int month = datePicker.getMonth() + 1; int year = datePicker.getYear();
我pipe理设置MinDate&MaxDate编程这样的:
final Calendar c = Calendar.getInstance(); int maxYear = c.get(Calendar.YEAR) - 20; // this year ( 2011 ) - 20 = 1991 int maxMonth = c.get(Calendar.MONTH); int maxDay = c.get(Calendar.DAY_OF_MONTH); int minYear = 1960; int minMonth = 0; // january int minDay = 25; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_account); BirthDateDP = (DatePicker) findViewById(R.id.create_account_BirthDate_DatePicker); BirthDateDP.init(maxYear - 10, maxMonth, maxDay, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (year < minYear) view.updateDate(minYear, minMonth, minDay); if (monthOfYear < minMonth && year == minYear) view.updateDate(minYear, minMonth, minDay); if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth) view.updateDate(minYear, minMonth, minDay); if (year > maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (monthOfYear > maxMonth && year == maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth) view.updateDate(maxYear, maxMonth, maxDay); }}); // BirthDateDP.init() } // activity
它适用于我,享受:)
你的意思是你想添加DatePicker小部件到你的应用程序。
将全局variables声明引入您的活动类:
private Button mPickDate; private int mYear; private int mMonth; private int mDay; static final int DATE_DIALOG_ID = 0;
写下这个代码到onCreate()函数中:
//date picker presentation mPickDate = (Button) findViewById(R.id.pickDate);//button for showing date picker dialog mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); // get the current date final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); // display the current date updateDisplay();
写下onCreate()函数之外的那些函数:
//return date picker dialog @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } //update month day year private void updateDisplay() { mBodyText.setText(//this is the edit text where you want to show the selected date new StringBuilder() // Month is 0 based so add 1 .append(mYear).append("-") .append(mMonth + 1).append("-") .append(mDay).append("")); //.append(mMonth + 1).append("-") //.append(mDay).append("-") //.append(mYear).append(" ")); } // the call back received when the user "sets" the date in the dialog private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } };
你也可以使用这个代码…
datePicker = (DatePicker) findViewById(R.id.schedule_datePicker); int day = datePicker.getDayOfMonth(); int month = datePicker.getMonth() + 1; int year = datePicker.getYear(); SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy"); Date d = new Date(year, month, day); String strDate = dateFormatter.format(d);
DatePicker类具有获取月份,年份,date的方法。 或者你可以使用OnDateChangedListener 。
你也可以使用te Calendar.GregorianCalendar java类
GregorianCalendar calendarBeg=new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(),datePicker.getDayOfMonth()); Date begin=calendarBeg.getTime();