效果图:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fpchx); final Calendar cd=Calendar.getInstance(); Date date=new Date(); cd.setTime(date); //选择日期 etKjrq.setOnClickListener(new OnClickListener(){ public void onClick(View v) { new DatePickerDialog(FpchxActivity.this, new OnDateSetListener(){ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { etKjrq.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show(); } }); etKjrq.setOnFocusChangeListener(new OnFocusChangeListener(){ public void onFocusChange(View v, boolean hasFocus) { new DatePickerDialog(FpchxActivity.this, new OnDateSetListener(){ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { etKjrq.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show(); } }); }