Android开发手记(9) DatePickerDialog 和 TimePickerDialog

1、DatePickerDialog 

  用于获取用户输入的日期信息。其原型为:

public DatePickerDialog(Contex contex, DatePickerDialog.OnDateSetListener callback, int year, int month, int day);

  其中,year,month,day是传递给DatePickerDialog的初始值信息,然后通过OnDateSetListener来设置用户选择的日期。

1  DatePickerDialog dpDlg = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
2                     @Override
3                     public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
4                         year = i;
5                         month = i1;
6                         day = i2;
7                         tvDate.setText(year + "-" + month + "-" + day);
8                     }
9                 },year,month,day);

  

  完整代码为:

 1 import android.app.DatePickerDialog;
 2 import android.support.v7.app.AppCompatActivity;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.widget.Button;
 6 import android.widget.DatePicker;
 7 import android.widget.TextView;
 8 
 9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.Locale;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     private TextView tvDate;
16     private Button btnDateDlg;
17     private int year;
18     private int month;
19     private int day;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         tvDate = (TextView) findViewById(R.id.textView);
27         btnDateDlg = (Button) findViewById(R.id.button);
28         Calendar myCalendar = Calendar.getInstance(Locale.CHINA);
29         Date date = new Date();
30         myCalendar.setTime(date);
31 
32         year = myCalendar.get(Calendar.YEAR);
33         month = myCalendar.get(Calendar.MONTH);
34         day = myCalendar.get(Calendar.DAY_OF_MONTH);
35         tvDate.setText(year + "-" + month + "-" + day);
36         btnDateDlg.setOnClickListener(new View.OnClickListener() {
37             @Override
38             public void onClick(View view) {
39                 DatePickerDialog dpDlg = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
40                     @Override
41                     public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
42                         year = i;
43                         month = i1;
44                         day = i2;
45                         tvDate.setText(year + "-" + month + "-" + day);
46                     }
47                 },year,month,day);
48                 dpDlg.show();
49             }
50         });
51 
52     }
53 
54 }

2、TimePickerDialog 

  用于获取用户输入的日期信息。其原型为:

public TimePickerDialog(Contex contex, TimePickerDialog.OnTimeSetListener callback, int hour, int minute, boolean is24Hours);

  其中,hour,minute是传递给TimePickerDialog的初始值信息,is24Hours表示是否为24小时制,然后通过OnTimeSetListener来设置用户选择的日期。

 

1 TimePickerDialog tpDlg = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
2                     @Override
3                     public void onTimeSet(TimePicker timePicker, int i, int i1) {
4                         hour = i;
5                         minute = i1;
6                         tvTime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
7                     }
8                 },hour, minute,true);
9                 tpDlg.show();

 

  

  完整代码为:

 1 import android.app.TimePickerDialog;
 2 import android.support.v7.app.AppCompatActivity;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.widget.Button;
 6 import android.widget.TextView;
 7 import android.widget.TimePicker;
 8 
 9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.Locale;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     private TextView tvTime;
16     private Button btnTimeDlg;
17     private int year;
18     private int month;
19     private int day;
20     private int hour;
21     private int minute;
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27 
28         tvTime = (TextView) findViewById(R.id.textView);
29         btnTimeDlg = (Button) findViewById(R.id.button);
30         Calendar myCalendar = Calendar.getInstance(Locale.CHINA);
31         Date date = new Date();
32         myCalendar.setTime(date);
33 
34         year = myCalendar.get(Calendar.YEAR);
35         month = myCalendar.get(Calendar.MONTH);
36         day = myCalendar.get(Calendar.DAY_OF_MONTH);
37         hour = myCalendar.get(Calendar.HOUR);
38         minute = myCalendar.get(Calendar.MINUTE);
39         tvTime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
40         btnTimeDlg.setOnClickListener(new View.OnClickListener() {
41             @Override
42             public void onClick(View view) {
43                 TimePickerDialog tpDlg = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
44                     @Override
45                     public void onTimeSet(TimePicker timePicker, int i, int i1) {
46                         hour = i;
47                         minute = i1;
48                         tvTime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
49                     }
50                 },hour, minute,true);
51                 tpDlg.show();
52             }
53         });
54 
55     }
56 
57 }

 

posted @ 2015-10-15 20:28  doodle777  阅读(457)  评论(0编辑  收藏  举报