我正在使用DatePicker小部件,我似乎无法让侦听器触发,我已经把日志放在各处看看发生了什么,但它似乎没有意识到日期已经改变
Main.java
DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
dp.init(year, month, day, new OnDateChangedListener (){
public void onDateChanged(DatePicker dp, int selectedYear, int monthOfYear, int dayOfMonth)
{
year = selectedYear;
month = monthOfYear;
day = dayOfMonth;
updateTime(year, month, day);
};
}); // setup initial values and reg. handler
以下是我在xml中的部分,仅供参考
<DatePicker
android:id="@+id/datePicker"
android:layout_width="fill_parent"
android:padding="5sp"
android:gravity="center"
android:layout_height="wrap_content">
</DatePicker>
答案 0 :(得分:0)
试试这个
DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
dp.init(year, month, day,datepickerListener);
并在DatePickerListener
中添加此代码
private DatePicker.OnDateChangedListener datepickerListener = new OnDateChangedListener(){
@Override
public void onDateSet(DatePicker view, int selectedYear, int monthOfYear, int dayOfMonth){
if (view.isShown()) {
dp.set(selectedYear, monthOfYear, dayOfMonth);
}
}
答案 1 :(得分:0)
@Brendon Rother ,试试这种方式。让我告知
// the date picker listener
OnDateChangedListener birthdayListener = new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker birthDayDatePicker,
int newYear, int newMonth, int newDay) {
try
{
// currentMonth + 1, to retrieve proper month
setHumanReadableDate(newYear, newMonth + 1, newDay);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
};