05
2017
10

DataPicker和TimePicker

2017.10.03
今天我学习了DataPicker和TimePicke的基本用法
注意事项,Diallog的方法后面要添加 show,不然不会显示
月份的开始是从零开始的,一般用month=calendar.get(Calendar.MONTH);加上1,看情况而定

public class MainActivity extends AppCompatActivity {
    private DatePicker datePicker;
    private TimePicker timePicker;
    private Calendar calendar;
    private int year;
    private int month;
    private  int day;
    private int hour;
    private int minute;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        hour=calendar.get(Calendar.HOUR);
        minute=calendar.get(Calendar.MINUTE);
        setTitle(year+"_"+(month+1)+"_"+day+"_"+hour+":"+minute);
        datePicker=(DatePicker)findViewById(R.id.datePicker3);
        timePicker=(TimePicker) findViewById(R.id.timePicker2);
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                setTitle(i+"_"+(i1+1)+"_"+i2);
            }
        });
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int i, int i1) {
                setTitle(i+":"+(i1+1));
            }
        });
       /* new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { setTitle(i+"_"+(i1+1)+"_"+i2); } },year,month,day).show();*/
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                setTitle(i+":"+(i1+1));
            }
        },hour,minute,true).show();
    }
}
上一篇:iOS 多任务下载(支持离线 下一篇:GridView布局