gpt4 book ai didi

android - 如何通过单击一个简单的按钮在 android 中显示日历

转载 作者:行者123 更新时间:2023-11-29 15:20:13 25 4
gpt4 key购买 nike

谁能帮助我如何通过单击一个简单的按钮在 android 中显示日历

我在 andriod 中有一个按钮,如果我们点击一​​个按钮,用户应该能够从日历中选择日期

最佳答案

您可以为此使用 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()));


}

关于android - 如何通过单击一个简单的按钮在 android 中显示日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com