我的代码如下。我想要的是标题根据我选择的日期而变化。
public class MainActivity extends ActionBarActivity {
private TimePicker timePicker;
private DatePicker datePicker;
private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH) + 1;
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
setTitle(year + "-" + month + "-" + day + "-" + hour + "-" + minute);
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
datePicker.init(year, month, day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
setTitle(year + "-" + monthOfYear + "-" + dayOfMonth);
}
});
}
}
当我在模拟器中运行它并更改日期时,标题永远不会改变。
答案 0 :(得分:0)
完全适用于我的日期选择器对话框示例:
//declaration of DatePickerDialog//
DatePickerDialog datePickerDialog;
//intailizing the datePicakerDialog//
datePickerDialog = new DatePickerDialog(this, dateSetListener, Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//DatePickerDialog listerner
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@SuppressWarnings("deprecation")
@Override
public void onDateSet(DatePicker view, int years, int monthOfYear,
int dayOfMonth) {
datePickerDialog.updateDate(
Calendar.getInstance().get(Calendar.YEAR), Calendar
.getInstance().get(Calendar.MONTH), Calendar
.getInstance().get(Calendar.DAY_OF_MONTH));
}
}
};
//Seting date on dismising thedate picker//
datePickerDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (!isDateSet) {
datePickerDialog.updateDate(Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
}
}
});
我希望此代码可以帮助您解决此问题
答案 1 :(得分:0)
我添加两个日志,一个在外面,另一个在里面。
Log.i("tag", "Outside");
datePicker.init(year, month, day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
Log.i("tag", "Go inside onDateChanged");
setTitle(year + "-" + monthOfYear + "-" + dayOfMonth);
}
});
然后我在模拟器中运行它,我得到如下结果。
无论我如何从模拟器更改日期,都没有日志信息说"进入日期更改"。