时间与日期

1. TimePicker 的使用方法

2. OnTimeChangedListener 的使用方法

3. DatePicker 的使用方法

4. AnalogClock 的使用方法

 

1. TimePicker 的使用方法

 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="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <TimePicker
12         android:id="@+id/firstTimePicker"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         />
16 
17 </RelativeLayout>

     

 

2. OnTimeChangedListener 的使用方法

 1 public static class PlaceholderFragment extends Fragment {
 2 
 3         private TimePicker firstTimePicker;
 4         public PlaceholderFragment() {
 5         }
 6 
 7         @Override
 8         public View onCreateView(LayoutInflater inflater, ViewGroup container,
 9                 Bundle savedInstanceState) {
10             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
11             
12             firstTimePicker = (TimePicker)rootView.findViewById(R.id.firstTimePicker);
13             
14             TimeListener timeListener = new TimeListener();
15             firstTimePicker.setOnTimeChangedListener(timeListener);
16             
17             return rootView;
18         }
19         
20         class TimeListener implements OnTimeChangedListener{
21 
22             @Override
23             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
24                 // TODO Auto-generated method stub
25                 Log.i("tag","Hour:"+ hourOfDay + ",Minute:" + minute);
26             }
27         }
28     }

        

 

         采用24小时制度的方法如下

 1 public static class PlaceholderFragment extends Fragment {
 2 
 3         private TimePicker firstTimePicker;
 4         public PlaceholderFragment() {
 5         }
 6 
 7         @Override
 8         public View onCreateView(LayoutInflater inflater, ViewGroup container,
 9                 Bundle savedInstanceState) {
10             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
11             
12             firstTimePicker = (TimePicker)rootView.findViewById(R.id.firstTimePicker);
13             firstTimePicker.setIs24HourView(true);  //24小时制
14             
15             TimeListener timeListener = new TimeListener();
16             firstTimePicker.setOnTimeChangedListener(timeListener);
17             
18             return rootView;
19         }
20         
21         class TimeListener implements OnTimeChangedListener{
22 
23             @Override
24             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
25                 // TODO Auto-generated method stub
26                 Log.i("tag","Hour:"+ hourOfDay + ",Minute:" + minute);
27             }
28         }
29     }

          

 

 

          也可以加上一个Button, 不用每次都打印出时间

 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="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <TimePicker
12         android:id="@+id/firstTimePicker"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         />
16     
17     <Button
18         android:id="@+id/firstButtonr"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:layout_below="@id/firstTimePicker"
22         android:text="确定"
23         />
24 
25 </RelativeLayout>
 1 public static class PlaceholderFragment extends Fragment {
 2 
 3         private TimePicker firstTimePicker;
 4         private Button button;
 5         public PlaceholderFragment() {
 6         }
 7 
 8         @Override
 9         public View onCreateView(LayoutInflater inflater, ViewGroup container,
10                 Bundle savedInstanceState) {
11             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
12             
13             firstTimePicker = (TimePicker)rootView.findViewById(R.id.firstTimePicker);
14             firstTimePicker.setIs24HourView(true); 
15             button = (Button)rootView.findViewById(R.id.firstButtonr);
16             
17             TimeListener timeListener = new TimeListener();
18             firstTimePicker.setOnTimeChangedListener(timeListener);
19             
20             ButtonListener buttonListener = new ButtonListener();
21             button.setOnClickListener(buttonListener);
22             
23             return rootView;
24         }
25         
26         class ButtonListener implements android.view.View.OnClickListener{
27 
28             @Override
29             public void onClick(View v) {
30                 int hour = firstTimePicker.getCurrentHour();  //获取当前时间
31                 int minute = firstTimePicker.getCurrentMinute();
32                 Log.i("tag","h:" + hour + ",m:" + minute);
33             }      
34         }
35         
36         class TimeListener implements OnTimeChangedListener{
37 
38             @Override
39             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
40                 // TODO Auto-generated method stub
41                 Log.i("tag","Hour:"+ hourOfDay + ",Minute:" + minute);
42             }
43         }
44     }

                  

 

3. DatePicker 的使用方法  

 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="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <DatePicker
12         android:id="@+id/firstDatePicker"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         />
16     
17     <Button
18         android:id="@+id/firstButton"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_below="@+id/firstDatePicker"
22         android:text="确定"
23         />
24 
25 </RelativeLayout>

     

 

      加入获取当前年月日的方法,这里不能直接用其监听器(原因后面会讲),所以火添加Button

 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="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <DatePicker
12         android:id="@+id/firstDatePicker"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         />
16     
17     <Button
18         android:id="@+id/firstButton"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_below="@+id/firstDatePicker"
22         android:text="确定"
23         />
24 
25 </RelativeLayout>
 1 package first.pack;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.support.v7.app.ActionBar;
 5 import android.support.v4.app.Fragment;
 6 import android.content.DialogInterface;
 7 import android.content.DialogInterface.OnClickListener;
 8 import android.media.MediaPlayer;
 9 import android.media.MediaPlayer.OnBufferingUpdateListener;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.LayoutInflater;
13 import android.view.Menu;
14 import android.view.MenuItem;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.Button;
18 import android.widget.DatePicker;
19 import android.widget.TimePicker;
20 import android.widget.TimePicker.OnTimeChangedListener;
21 import android.os.Build;
22 
23 public class MainActivity extends ActionBarActivity {
24 
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29 
30         if (savedInstanceState == null) {
31             getSupportFragmentManager().beginTransaction()
32                     .add(R.id.container, new PlaceholderFragment())
33                     .commit();
34         }
35     }
36 
37 
38     @Override
39     public boolean onCreateOptionsMenu(Menu menu) {
40         
41         // Inflate the menu; this adds items to the action bar if it is present.
42         getMenuInflater().inflate(R.menu.main, menu);
43         return true;
44     }
45 
46     @Override
47     public boolean onOptionsItemSelected(MenuItem item) {
48         // Handle action bar item clicks here. The action bar will
49         // automatically handle clicks on the Home/Up button, so long
50         // as you specify a parent activity in AndroidManifest.xml.
51         int id = item.getItemId();
52         if (id == R.id.action_settings) {
53             return true;
54         }
55         return super.onOptionsItemSelected(item);
56     }
57 
58     /**
59      * A placeholder fragment containing a simple view.
60      */
61     public static class PlaceholderFragment extends Fragment {
62 
63         private DatePicker datePicker;
64         private Button button;
65         public PlaceholderFragment() {
66         }
67 
68         @Override
69         public View onCreateView(LayoutInflater inflater, ViewGroup container,
70                 Bundle savedInstanceState) {
71             View rootView = inflater.inflate(R.layout.fragment_main, container, false);        
72             
73             datePicker = (DatePicker)rootView.findViewById(R.id.firstDatePicker);
74             button = (Button)rootView.findViewById(R.id.firstButton);
75             
76             ButtonListener buttonListener = new ButtonListener();
77             button.setOnClickListener(buttonListener);
78                          
79             return rootView;
80         }
81         
82         class ButtonListener implements android.view.View.OnClickListener{
83 
84             @Override
85             public void onClick(View v) {
86                 int year = datePicker.getYear();
87                 int month = datePicker.getMonth();
88                 int day = datePicker.getDayOfMonth();
89                 Log.i("tag","year:" + year + ",month:" + month + ",day" + day);
90             }         
91         }
92     }
93 }

 

         这里的月份是特殊的,它是从0开始到11月,所以哦July是第6个月!!!!

 

4. AnalogClock 的使用方法

         用的不多, <AnalogClock />则是添加时钟

         也可以查下数字时钟是如何添加的

      

posted @ 2014-07-04 12:07  Mirrorhanman  阅读(232)  评论(0编辑  收藏  举报