1、日期选择器:java部分代码:
<DatePicker
android:id="@+id/datapicker"
android:layout_width="match_parent"
android:layout_height="match_parent"></DatePicker>
获取日期java部分代码:
package com.example.lenovo.android; import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.RadioGroup; import android.widget.Toast; import java.util.Calendar; public class MainActivity extends Activity { int year,month,day; DatePicker datePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datePicker=(DatePicker)findViewById(R.id.datapicker); Calendar calendar=Calendar.getInstance(); year=calendar.get(Calendar.YEAR); month=calendar.get(Calendar.MONTH); day=calendar.get(Calendar.DAY_OF_MONTH); datePicker.init(year,month,day,new DatePicker.OnDateChangedListener(){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { MainActivity.this.year=year; MainActivity.this.month=monthOfYear; MainActivity.this.day=dayOfMonth; show(year,monthOfYear,dayOfMonth); } }); } private void show(int year,int month,int day){ String str=year+"年"+(month+1)+"月"+day+"日"; Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show(); } }
2、时间选择器:
<TimePicker
android:id="@+id/timepick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TimePicker>
获取时间的java代码与上面相似