标签:
作用:DatePicker组件可以输入日期。范围是1900-1-1~2100-12-31
对应的获取日期方法datepicker.getYear() ,datepicker.getMonth(),datepicker.getDayOfMonth()
使用:
<DatePicker android:id="@+id/date_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" />
代码:
public class MainActivity extends Activity { private DatePicker datePicker; private int year; private int monthOfYear; private int dayOfMonth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化 datePicker = (DatePicker) findViewById(R.id.date_picker); // 获取系统时间 Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // DatePicker的事件监听 datePicker.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText( getApplicationContext(), year + "" + (monthOfYear + 1) + "" + dayOfMonth + "", Toast.LENGTH_SHORT).show(); } }); } }
备注:
这是静态的使用,可以用动态的使用,请参考《静态和动态组件的选择》
标签:
原文地址:http://www.cnblogs.com/H-BolinBlog/p/5290253.html