效果图:
MainActivity.java
package com.jkxqj.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.DatePicker; public class MainActivity extends Activity { private DatePicker datePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datePicker = (DatePicker)findViewById(R.id.firstDatePicker); datePicker.updateDate(2013, 8, 23); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { int y = datePicker.getYear(); int m = datePicker.getMonth(); int d = datePicker.getDayOfMonth(); System.out.println("y:" + y + ",m" + m + ",d:" + d); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.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=".MainActivity" > <AnalogClock android:id="@+id/clock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="64dp" /> <DatePicker android:id="@+id/firstDatePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/clock" android:layout_centerHorizontal="true" android:layout_marginTop="17dp" /> </RelativeLayout>