android學習筆記21:日期時間控件 – Android移動開發技術文章_手機開發 Android移動開發教學課程

android中的日期控件為:DatePicker,時間控件為TimePicker。通過指定兩個監聽器OnDateChangelistner和OnTimeChangeListner就可以監控這兩個控件的改變,從而得到相應的時間。

 

 

<?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="選擇您出生的具體時間" 
    /> 
<!– 定義一個DatePicker組件 –> 
<DatePicker android:id="@+id/datePicker" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    /> 
<!– 定義一個TimePicker組件 –> 
<TimePicker android:id="@+id/timePicker" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    /> 
<!– 顯示用戶輸入日期、時間的控件 –> 
<EditText android:id="@+id/show" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:editable="false" 
    android:cursorVisible="false" 
    />    
</LinearLayout> 
<?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="選擇您出生的具體時間"
 />
<!– 定義一個DatePicker組件 –>
<DatePicker android:id="@+id/datePicker"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 />
<!– 定義一個TimePicker組件 –>
<TimePicker android:id="@+id/timePicker"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 />
<!– 顯示用戶輸入日期、時間的控件 –>
<EditText android:id="@+id/show"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:editable="false"
 android:cursorVisible="false"
 /> 
</LinearLayout>

public class ChooseDate extends Activity 

    //定義5個記錄當前時間的變量  
    private int year; 
    private int month; 
    private int day; 
    private int hour; 
    private int minute; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker); 
        TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker); 
        //獲取當前的年、月、日、小時、分鐘  
        Calendar c = Calendar.getInstance(); 
        year = c.get(Calendar.YEAR); 
        month = c.get(Calendar.MONTH); 
        day = c.get(Calendar.DAY_OF_MONTH); 
        hour = c.get(Calendar.HOUR); 
        minute = c.get(Calendar.MINUTE); 
        //初始化DatePicker組件,初始化時指定監聽器  
        datePicker.init(year , month ,day  
            , new OnDateChangedListener() 
        { 
 
            @Override 
            public void onDateChanged(DatePicker arg0, int year 
                , int month, int day) 
            { 
                ChooseDate.this.year = year; 
                ChooseDate.this.month = month; 
                ChooseDate.this.day = day; 
                //顯示當前日期、時間  
                showDate(year, month , day , hour, minute); 
            } 
        }); 
        //為TimePicker指定監聽器  
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() 
        { 
            @Override 
            public void onTimeChanged(TimePicker arg0, int hour, int minute) 
            { 
                ChooseDate.this.hour = hour; 
                ChooseDate.this.minute = minute; 
                //顯示當前日期、時間  
                showDate(year, month , day , hour, minute);              
            } 
        }); 
    } 
    //定義在EditText中顯示當前日期、時間的方法  
    private void showDate(int year, int month , int day 
            , int hour , int minute) 
    { 
        EditText show = (EditText)findViewById(R.id.show); 
        show.setText("您的出生日期為:" + year + "年" + (month+1) + "月" 
            + day + "日  " + hour + "時" + minute + "分"); 
    }    

public class ChooseDate extends Activity
{
 //定義5個記錄當前時間的變量
 private int year;
 private int month;
 private int day;
 private int hour;
 private int minute;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
  TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
  //獲取當前的年、月、日、小時、分鐘
  Calendar c = Calendar.getInstance();
  year = c.get(Calendar.YEAR);
  month = c.get(Calendar.MONTH);
  day = c.get(Calendar.DAY_OF_MONTH);
  hour = c.get(Calendar.HOUR);
  minute = c.get(Calendar.MINUTE);
  //初始化DatePicker組件,初始化時指定監聽器
  datePicker.init(year , month ,day
   , new OnDateChangedListener()
  {

   @Override
   public void onDateChanged(DatePicker arg0, int year
    , int month, int day)
   {
    ChooseDate.this.year = year;
    ChooseDate.this.month = month;
    ChooseDate.this.day = day;
    //顯示當前日期、時間
    showDate(year, month , day , hour, minute);
   }
  });
  //為TimePicker指定監聽器
  timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
  {
   @Override
   public void onTimeChanged(TimePicker arg0, int hour, int minute)
   {
    ChooseDate.this.hour = hour;
    ChooseDate.this.minute = minute;
    //顯示當前日期、時間
    showDate(year, month , day , hour, minute);    
   }
  });
 }
 //定義在EditText中顯示當前日期、時間的方法
 private void showDate(int year, int month , int day
   , int hour , int minute)
 {
  EditText show = (EditText)findViewById(R.id.show);
  show.setText("您的出生日期為:" + year + "年" + (month+1) + "月"
   + day + "日  " + hour + "時" + minute + "分");
 } 
}

摘自 snoopy的專欄

發佈留言