如何重置datepicker

时间:2011-11-24 05:55:25

标签: android datetime

我正在Android 2.1上开发一个任务管理器。 我想在单击重置按钮到当前日期和时间时重置日期和时间。 帮我解决代码..

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText next = (EditText) findViewById(R.id.editText1);
final Button sub = (Button) findViewById(R.id.button1);
final Button res = (Button) findViewById(R.id.button2);
final DatePicker dp= (DatePicker) findViewById(R.id.datePicker1);
final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

res.setOnClickListener(new View.OnClickListener() {     
 public void onClick(final View view) {                  
     next.setText("");
     dp.refreshDrawableState();
        }       
    });    
}}

2 个答案:

答案 0 :(得分:5)

//get current time
Time now = new Time();
now.setToNow();

//update the TimePicker
tp.setHour(now.hour);
tp.setMinute(now.minute);

//update the DatePicker
dp.updateDate(now.year, now.month, now.monthDay);

答案 1 :(得分:0)

Time现已弃用。请改用Calendar

Calendar calendar = Calendar.getInstance();

tp.setHour(calendar.get(Calendar.HOUR_OF_DAY));
tp.setMinute(calendar.get(Calendar.MINUTE));      

dp.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));