今天学习了日期选择器的使用。
代码如下:
public class MainActivity extends AppCompatActivity implements DatePicker.OnDateChangedListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DatePicker dp_test = (DatePicker) findViewById(R.id.dp_test); Calendar calendar = Calendar.getInstance(); int year=calendar.get(Calendar.YEAR); int monthOfYear=calendar.get(Calendar.MONTH); int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH); dp_test.init(year,monthOfYear,dayOfMonth,this); } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this,"您选择的日期是:"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日!",Toast.LENGTH_SHORT).show(); } }