任何人都可以通过点击一个简单的按钮来帮助我如何在Android中显示日历
我在andriod中有一个按钮,如果我们点击按钮,用户应该可以从日历中选择日期
答案 0 :(得分:4)
您可以使用DatePickerDialog为我发布示例代码
static final int DATE_DIALOG_ID = 999;
DatePickerDialog.OnDateSetListener date;
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(val);
}
};
depart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
val = 1;
}
});
protected Dialog onCreateDialog(int id) {
final Calendar now = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)) {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (year - now.get(Calendar.YEAR) > 1) {
view.updateDate(year - 1,
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear > now.get(Calendar.MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear == now.get(Calendar.MONTH)
&& dayOfMonth > now.get(Calendar.DAY_OF_MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year < now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (monthOfYear < now.get(Calendar.MONTH)
&& year == now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH)
&& year == now.get(Calendar.YEAR)
&& monthOfYear == now.get(Calendar.MONTH))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
};
return _date;
}
return null;
}
设置日期:
private void updateLabel(int val) {
String myFormat = "dd/MM/yy"; // In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Log.d("Date value ", "===" + sdf.format(myCalendar.getTime()));
depart.setText(sdf.format(myCalendar.getTime()));
}
答案 1 :(得分:1)
要从按钮点击事件打开日历,请将此代码添加到onClick方法:
Intent i = new Intent();
//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn);
startActivity(i);
答案 2 :(得分:0)