標籤:
1 package com.example.myact5; 2 3 import java.util.Calendar; 4 5 import android.app.DatePickerDialog; 6 import android.app.DatePickerDialog.OnDateSetListener; 7 import android.app.TimePickerDialog; 8 import android.app.TimePickerDialog.OnTimeSetListener; 9 import android.os.Bundle;10 import android.support.v7.app.ActionBarActivity;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 import android.widget.DatePicker;15 import android.widget.TimePicker;16 import android.widget.Toast;17 /**18 * 19 * @Describe: 20 * @package: com.example.myact521 * @author shaobn22 * @date 2015-9-11 上午9:10:2823 */24 public class MainActivity extends ActionBarActivity {25 private Button button;26 private Button button2;27 private Calendar calendar = Calendar.getInstance();28 @Override29 protected void onCreate(Bundle savedInstanceState) {30 super.onCreate(savedInstanceState);31 setContentView(R.layout.activity_main);32 button = (Button) this.findViewById(R.id.button1);33 button2 = (Button) this.findViewById(R.id.button2);34 button.setOnClickListener(new OnClickListener() {35 36 @Override37 public void onClick(View arg0) {38 // TODO Auto-generated method stub39 TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {40 41 @Override42 public void onTimeSet(TimePicker arg0, int arg1, int arg2) {43 // TODO Auto-generated method stub44 Toast.makeText(MainActivity.this,getConstantInstance().hourOfDay+","+ getConstantInstance().minute, 1).show();45 }46 }, getConstantInstance().hourOfDay, getConstantInstance().minute, true); 47 tpd.show();48 }49 });50 button2.setOnClickListener(new OnClickListener() {51 52 @Override53 public void onClick(View arg0) {54 // TODO Auto-generated method stub55 DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {56 57 @Override58 public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {59 // TODO Auto-generated method stub60 Toast.makeText(MainActivity.this, arg1+","+(arg2+1)+","+arg3, 1).show();61 }62 }, getConstantInstance().year, getConstantInstance().monthOfYear, getConstantInstance().dayOfMonth);63 dpd.show();64 }65 });66 } 67 class Constant{68 public int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);69 public int minute = calendar.get(Calendar.MINUTE);70 public boolean is24HourView = true;71 public int year = calendar.get(Calendar.YEAR);72 public int monthOfYear = calendar.get(Calendar.MONTH);73 public int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);74 }75 public Constant getConstantInstance(){76 return new Constant();77 }78 }
筆者寫的麻煩點,為了練習一下。
Android DatePickerDialog TimepickerDialog