日期選擇器(DataPicker)android
顯示一個可供日期選擇的界面佈局
監聽器方法init(year,month,day,OnDateChangedListener)this
監聽器spa
DataPicker.OnDateChangedListener3d
下面咱們直接看代碼:code
1.Activityxml
//日期選擇器 public class DatePickerActivity extends Activity { private DatePicker datePicker; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.date_picker); datePicker = (DatePicker)findViewById(R.id.datePickerId); //獲取日曆 Calendar c1 = Calendar.getInstance(); // 設置最小的時間年份 c1.set(1990, 0, 1, 0, 0, 0); datePicker.setMinDate(c1.getTimeInMillis()); // 設置最大的時間年份 Calendar c2 = Calendar.getInstance(); c2.set(2020, 11, 31, 23, 59, 59); datePicker.setMaxDate(c2.getTimeInMillis()); //datePicker.init(year, monthOfYear, dayOfMonth, onDateChangedListener)監聽器方法 //DatePicker.OnDateChangedListener監聽器 datePicker.init(1993,0,1,new DatePicker.OnDateChangedListener() { public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(DatePickerActivity.this, "你選擇的日期:"+(year+"-"+(monthOfYear+1)+"-"+dayOfMonth), Toast.LENGTH_SHORT).show(); } }); } }
2.xml佈局文件blog
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <!-- 日期選擇器 --> <DatePicker android:id="@+id/datePickerId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
3.效果圖顯示utf-8