我在Android中使用DatePicker
小部件供用户设置日期,并且想要在点击确认按钮时获取日期值,我该怎么做?
答案 0 :(得分:80)
试试这个:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
答案 1 :(得分:22)
我成功设置了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
它适用于我,享受:)
答案 2 :(得分:5)
您的意思是您想将DatePicker小部件添加到您的应用中。
您的活动类中的全局变量声明:
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();
}
};
答案 3 :(得分:3)
您也可以使用此代码...
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);
答案 4 :(得分:3)
如果您使用的是科特林,则可以为DatePicker
定义一个extension function:
fun DatePicker.getDate(): Date {
val calendar = Calendar.getInstance()
calendar.set(year, month, dayOfMonth)
return calendar.time
}
然后,它就是:datePicker.getDate()
。好像它一直存在。
答案 5 :(得分:1)
DatePicker课程有获取月,日,月的方法。或者您可以使用OnDateChangedListener。
答案 6 :(得分:1)
你也可以使用te Calendar.GregorianCalendar java class
var counter = 0;
$( "#fixedFooter").click(function(){
if (counter == 0){
$("#fixedFooter").animate({height: "200px"});
counter++;
}
else if (counter == 1){
$("#fixedFooter").animate({height: "100%"});
counter++;
}
else if (counter == 2){
$("#fixedFooter").animate({height: "20px"});
counter = 0;
}
});