当前位置:编程学习 > JAVA >>

android中timepicker怎么保存设置好的时间


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);

/* 获取当前时间和日期*/
Calendar 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);
datepicker=(DatePicker)findViewById(R.id.datepicker);
timepicker=(TimePicker)findViewById(R.id.timepicker);
/* 设置TextView对象,显示初始日期时间*/
textview=(TextView)findViewById(R.id.timeview);
textview.setText(new StringBuilder().append(year).append("/").append(format(month+1)).append("/").append(format(day)).append(" ").append(format(hour)).append(":").append(format(minute)));
/*设置OnDateChangedListener*/
datepicker.init(year,month,day,new DatePicker.OnDateChangedListener() {

public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
TimeActivity.this.year=year;month=monthOfYear;day=dayOfMonth;
textview.setText(new StringBuilder().append(year).append("/").append(format(month+1)).append("/").append(format(day)).append(" ").append(format(hour)).append(":").append(format(minute)));
}
});
timepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour=hourOfDay;
TimeActivity.this.minute=minute;
textview.setText(new StringBuilder().append(year).append("/").append(format(month+1)).append("/").append(format(day)).append(" ").append(format(hour)).append(":").append(format(minute)));
//就是这里!!请看这里!!到底怎么保存设置的时间呢?每次修改时间退出后再进来还是会回去原来的跟系统一样的时间}

});

}
private String format(int time){
String str=" "+time;
if(str.length()==1)str="0"+str;
return str;
}
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,