如何通过单击一个简单的按钮在 android 中显示日历
Posted
技术标签:
【中文标题】如何通过单击一个简单的按钮在 android 中显示日历【英文标题】:How to Show a Calender in android by clicking a simple button 【发布时间】:2013-10-05 21:34:23 【问题描述】:任何人都可以通过单击一个简单的按钮来帮助我如何在 android 中显示日历
我在安卓中有一个按钮,如果我们点击一个按钮,用户应该能够从日历中选择日期
【问题讨论】:
你的意思是日期选择器或日历应用程序? 【参考方案1】:您可以为此使用 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()));
【讨论】:
【参考方案2】:要从按钮单击事件打开日历,请将此代码添加到 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);
【讨论】:
【参考方案3】:如果您指的是日期选择器,那么您可以检查this。
或者您可以使用this 库作为最新版本的日期选择器。
【讨论】:
以上是关于如何通过单击一个简单的按钮在 android 中显示日历的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击 Android 中的按钮使 VideoView 全屏显示?
Android - 在按钮单击时取消静音 VideoView