DatePickerDialog的使用

来源:互联网 发布:天龙八部男生发色数据 编辑:程序博客网 时间:2024/04/30 08:20

DatePickerDialog的使用。今天在做一个有关日期的功能,实现的效果是点击editText就打开选取日期界面,选好之后按确定,然后editText显示XXXX年XX月XX日的效果。

实现效果如下:


具体实现步骤如下:

main.xml

<TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="生产日期" />                <EditText                    android:id="@+id/produceDateET"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:editable="false"                    android:focusable="false"                    android:hint="点击选择日期">                </EditText>


main.java

produceDateET.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(final View v) {// 生成一个DatePickerDialog对象,并显示。显示的DatePickerDialog控件可以选择年月日,并设置DatePickerDialog(v);}});/** * 生成的datePiackerDialo对象 *  * @param v */protected void DatePickerDialog(final View v) {// 生成一个DatePickerDialog对象,并显示。显示的DatePickerDialog控件可以选择年月日,并设置new DatePickerDialog(activity,new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {// 回调函数,同时设置控件值AttachmentCollectionActivity.dateInfo.set(Calendar.YEAR, year);dateInfo.set(Calendar.MONTH, monthOfYear);dateInfo.set(Calendar.DAY_OF_MONTH, dayOfMonth);((EditText) v).setText(DateFormat.getDateInstance().format((dateInfo.getTime())));}}, dateInfo.get(Calendar.YEAR), dateInfo.get(Calendar.MONTH),dateInfo.get(Calendar.DAY_OF_MONTH)).show();}


0 0