ANDROID_MARS学习笔记_S02_002_Date\TimePicker
一、文档用法
1.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.s02_e02_dateandtimepickerdialog.MainActivity" > 10 11 <Button 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="pick_date" 15 android:onClick="showDatePickerDialog" /> 16 17 </RelativeLayout>
2.java
1 package com.example.s02_e02_dateandtimepickerdialog; 2 3 import java.util.Calendar; 4 5 import android.annotation.SuppressLint; 6 import android.app.Activity; 7 import android.app.DatePickerDialog; 8 import android.app.Dialog; 9 import android.app.DialogFragment; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.widget.DatePicker; 13 14 @SuppressLint("NewApi") 15 public class MainActivity extends Activity { 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 } 22 23 public void showDatePickerDialog(View v) { 24 DialogFragment newFragment = new DatePickerFragment(); 25 newFragment.show(getFragmentManager(), "datePicker"); 26 } 27 28 @SuppressLint("NewApi") 29 public static class DatePickerFragment extends DialogFragment implements 30 DatePickerDialog.OnDateSetListener { 31 32 @Override 33 public Dialog onCreateDialog(Bundle savedInstanceState) { 34 // Use the current date as the default date in the picker 35 final Calendar c = Calendar.getInstance(); 36 int year = c.get(Calendar.YEAR); 37 int month = c.get(Calendar.MONTH); 38 int day = c.get(Calendar.DAY_OF_MONTH); 39 40 // Create a new instance of DatePickerDialog and return it 41 return new DatePickerDialog(getActivity(), this, year, month, day); 42 } 43 44 public void onDateSet(DatePicker view, int year, int month, int day) { 45 // Do something with the date chosen by the user 46 } 47 } 48 49 }
二、教程用法
1.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/textViewId" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/hello" 12 /> 13 <Button 14 android:id="@+id/showDatePickerButton" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="显示DatePicker" 18 /> 19 20 </LinearLayout>
2.java
1 package mars.dateandtime; 2 3 import android.app.Activity; 4 import android.app.DatePickerDialog; 5 import android.app.Dialog; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.DatePicker; 11 12 public class MainActivity extends Activity { 13 /** Called when the activity is first created. */ 14 private Button showDatePickerButton = null; 15 //该常量用于标识DatePickerDialog 16 private static final int DATE_PICKER_ID = 1; 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 showDatePickerButton = (Button) findViewById(R.id.showDatePickerButton); 22 showDatePickerButton.setOnClickListener(new ButtonListener()); 23 } 24 25 private class ButtonListener implements OnClickListener { 26 27 @Override 28 public void onClick(View v) { 29 //此方法用于显示DatePickerDialog 30 showDialog(DATE_PICKER_ID); 31 } 32 33 } 34 //监听器,用户监听用户点下DatePikerDialog的set按钮时,所设置的年月日 35 DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() { 36 37 @Override 38 public void onDateSet(DatePicker view, int year, int monthOfYear, 39 int dayOfMonth) { 40 System.out.println(year + "-" + monthOfYear + "-" + dayOfMonth); 41 } 42 }; 43 44 @Override 45 protected Dialog onCreateDialog(int id) { 46 switch (id) { 47 case DATE_PICKER_ID: 48 return new DatePickerDialog(this, onDateSetListener, 2010, 11, 25); 49 } 50 return null; 51 } 52 53 }
You can do anything you set your mind to, man!