标签:
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.s01_e16_time.MainActivity" > 10 11 <!-- 12 <TimePicker 13 android:id="@+id/firstTimePicker" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="@string/hello_world" /> 17 18 <Button 19 android:id="@+id/timeButton" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:layout_below="@id/firstTimePicker"/> 23 24 <DatePicker 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:id="@+id/firstDatePicker"/> 28 29 <Button 30 android:id="@+id/dateButton" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_below="@id/firstDatePicker"/> 34 --> 35 <AnalogClock 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:id="@+id/clockId"/> 39 40 </RelativeLayout>
2.java
1 @SuppressLint("NewApi") 2 public class MainActivity extends ActionBarActivity { 3 private TimePicker firstTimePikcer; 4 private Button button; 5 private Button dateButton; 6 private DatePicker firstDatePicker; 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 // firstTimePikcer = (TimePicker) findViewById(R.id.firstTimePicker); 12 // firstTimePikcer.setIs24HourView(true); 13 // firstTimePikcer.setOnTimeChangedListener(new TimeListener()); 14 // 15 // button = (Button) findViewById(R.id.timeButton); 16 // button.setOnClickListener(new ButtonListener()); 17 18 // firstDatePicker = (DatePicker) findViewById(R.id.firstDatePicker); 19 // dateButton = (Button) findViewById(R.id.dateButton); 20 // dateButton.setOnClickListener(new DateButtonListener()); 21 } 22 23 class TimeListener implements OnTimeChangedListener { 24 25 @Override 26 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 27 System.out.println(hourOfDay+":"+minute); 28 } 29 30 } 31 32 class ButtonListener implements OnClickListener { 33 34 @Override 35 public void onClick(View v) { 36 int hour = firstTimePikcer.getCurrentHour(); 37 int minute = firstTimePikcer.getCurrentMinute(); 38 System.out.println(hour+"---"+minute); 39 } 40 41 } 42 class DateButtonListener implements OnClickListener { 43 44 @Override 45 public void onClick(View v) { 46 int y = firstDatePicker.getYear(); 47 int m = firstDatePicker.getMonth(); 48 int d = firstDatePicker.getDayOfMonth(); 49 System.out.println(y+"-"+m+"-"+d); 50 } 51 52 }
ANDROID_MARS学习笔记_S01_010日期时间控件
标签:
原文地址:http://www.cnblogs.com/shamgod/p/5186944.html