1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.s01_e16_time.MainActivity" > <!--
<TimePicker
android:id="@+id/firstTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/timeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstTimePicker"/> <DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstDatePicker"/> <Button
android:id="@+id/dateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstDatePicker"/>
-->
<AnalogClock
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/clockId"/> </RelativeLayout>
2.java
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
private TimePicker firstTimePikcer;
private Button button;
private Button dateButton;
private DatePicker firstDatePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// firstTimePikcer = (TimePicker) findViewById(R.id.firstTimePicker);
// firstTimePikcer.setIs24HourView(true);
// firstTimePikcer.setOnTimeChangedListener(new TimeListener());
//
// button = (Button) findViewById(R.id.timeButton);
// button.setOnClickListener(new ButtonListener()); // firstDatePicker = (DatePicker) findViewById(R.id.firstDatePicker);
// dateButton = (Button) findViewById(R.id.dateButton);
// dateButton.setOnClickListener(new DateButtonListener());
} class TimeListener implements OnTimeChangedListener { @Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
System.out.println(hourOfDay+":"+minute);
} } class ButtonListener implements OnClickListener { @Override
public void onClick(View v) {
int hour = firstTimePikcer.getCurrentHour();
int minute = firstTimePikcer.getCurrentMinute();
System.out.println(hour+"---"+minute);
} }
class DateButtonListener implements OnClickListener { @Override
public void onClick(View v) {
int y = firstDatePicker.getYear();
int m = firstDatePicker.getMonth();
int d = firstDatePicker.getDayOfMonth();
System.out.println(y+"-"+m+"-"+d);
} }