1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
findViewById(R.id.btn_pick_date).setOnClickListener(this); findViewById(R.id.btn_pick_time).setOnClickListener(this); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_pick_date: Calendar cale1 = Calendar.getInstance(); new DatePickerDialog( MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { String result = ""; result += "你选择的日期是:" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); } }, cale1.get(Calendar.YEAR), cale1.get(Calendar.MONTH), cale1.get(Calendar.DAY_OF_MONTH) ).show(); break; case R.id.btn_pick_time: Calendar cale2 = Calendar.getInstance(); new TimePickerDialog( MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String result = ""; result += "你选择的时间是:" + hourOfDay + "时" + minute + "分"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); } }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true ).show(); break; } } }
|