DatePicker介绍
DatePicker 继承FrameLayout类,
日期选择控件的主要功能是向用户提供包含年、月、日得日期数据并允许用户对其修改。
如果要捕获用户修改日期选择控件的数据事件响应,需要为DatePicker添加一个OnDateChangedListener监听器.
1.重要方法
getDayOfMonth():获取当前Day
getMonth(): 获取当前月(注意:返回数值为0..11,需要自己+1来显示)
getYear() 获取当前年
updateDate(int year, int monthOfYear, int dayOfMonth):更新日
init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
参数
year 初始年(译者注:注意使用new Date()初始化年时,需要+1900,如下:date.getYear() + 1900)
monthOfYear 初始月。
dayOfMonth 初始日。
onDateChangedListener 日期改变时通知用户的事件监听,可以为空(null)。
效果图
本程序main.xml源码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="日期选择控件" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<DatePicker android:id="@+id/datePicker" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> </LinearLayout>
Java源码
package com.sx.DatePicker; import java.util.Calendar; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TextView; public class DatePickerActivity extends Activity { private DatePicker datePicker = null; private TextView textView = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); datePicker = (DatePicker)findViewById(R.id.datePicker); textView = (TextView) findViewById(R.id.textView); //创建一个日历引用calendar,通过静态方法getInstance() 从指定时区 Locale.CHINA 获得一个日期实例 Calendar calendar = Calendar.getInstance(Locale.CHINA); int year = calendar.get(Calendar.YEAR); int monthOfYear = calendar.get(Calendar.MONTH); int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); datePicker.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { textView.setText("你选择的日期是: " + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日"); } }); } }
以对话框形式显示日期视图效果图
java源码实现
package com.sx.DatePickerDialog; import java.util.Calendar; import java.util.Locale; import android.app.Activity; import android.app.DatePickerDialog; import android.os.Bundle; import android.widget.DatePicker; public class DatePickerDialogActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Calendar calendar = Calendar.getInstance(Locale.CHINA); final int year = calendar.get(Calendar.YEAR); final int month = calendar.get(Calendar.MONTH); final int day = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(this, setting, year, month, day); datePickerDialog.show(); } //当点击DatePickerDialog控件的设置按钮时,调用该方法 DatePickerDialog.OnDateSetListener setting = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) { setTitle("你按下了日期设置键"); } }; }