DatePickerDialog日历对话框/选择器
2020-04-05 本文已影响0人
android_Pie
DatePickerDialog日历对话框/选择器
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
private String[] weeks={"星期日","星期一","星期二","星期三","星期四", "星期五","星期六",
};
public void onClick01(final View v){
//获得日历对象
final Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int monthOfYear=c.get(Calendar.MONTH);
int dayOfMonth=c.get(Calendar.DAY_OF_MONTH);
//创建日历对话框
DatePickerDialog dialog=new DatePickerDialog(this,new OnDateSetListener() {
@Override
public void onDateSet(
DatePicker view, int year, int monthOfYear,int dayOfMonth) {
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
int week=c.get(Calendar.DAY_OF_WEEK);
((Button)v).
setText(year+"-"+$(monthOfYear+1)+"-"+$(dayOfMonth)+weeks[week-1]);
}
}, year, monthOfYear,dayOfMonth);
//显示对话框
dialog.show();
}
private String $(int n){//两位数以下加0字符
return n<10?"0"+
n:String.valueOf(n);
}