6

一旦用户开始更改它,我希望能够检测到日期选择器上的更改。但是,我不想使用 datrpickerdialog。有谁知道为什么当我更改 datepicker 上的日期时 onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) 不会触发?

我非常感谢您对此发表任何评论。干杯

这是我的代码的简化版本:

public class AddItem extends Activity  implements OnDateChangedListener,OnDateSetListener{

DatePicker start ;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryelements);
    start = (DatePicker) findViewById(R.id.start);

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {

    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
            " thiv view"+year,
            Toast.LENGTH_LONG).show();
    Log.v("indatechange", "ok");

}

@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
            " thiv view"+arg0,
            Toast.LENGTH_LONG).show();
    Log.v("indatechange", "ok");
}

}

4

4 回答 4

8

您需要告诉 DatePicker 您的 OnDateChangedListener。与常规小部件不同,DatePicker 没有setOnDateChangedListener()……但它确实有init(). 用于init()设置默认日期和一起声明监听器:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryelements);

    // Set the date to now
    Calendar calendar = Calendar.getInstance();
    start = (DatePicker) findViewById(R.id.start);
    start.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this);
}
于 2012-11-09T16:55:47.250 回答
0
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entryelements);
start = (DatePicker) findViewById(R.id.start);

expirationDate=(EditText) findViewById(R.id.expirationDate);
Calendar calendar = Calendar.getInstance();
start = (DatePicker) findViewById(R.id.start);
MyOnDateChangeListener onDateChangeListener = new MyOnDateChangeListener();
start.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), onDateChangeListener);
}

public class MyOnDateChangeListener implements OnDateChangedListener {
    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        int mon=month+1;
        expirationDate.setText(day+"/"+mon+"/"+year);
    }
}
于 2015-02-16T08:25:56.610 回答
0

如果您想主动了解交互,则需要在 DatePicker 上调用init(year, month, day, onDateChangedListener)方法进行设置。

假设你想初始化它以显示一个特定的开始日期,调用它而不是它的 updateDate 方法是有意义的(这是你在做什么?或者你不关心起始值是什么?)

请注意,它不像 DatePickerDialog 那样支持 OnDateSetListener,因为这意味着 UI 可以通过某种方式知道用户何时完成与选取器的交互。因为它不是对话,所以当你认为它们完成时,你必须自己决定。只需跟踪 OnDateChangedListener,它就会有意义。

于 2012-11-10T02:53:13.963 回答
0
DatePicker dpEndDate,dpStartDate;
dpStartDate=(DatePicker)findViewById(R.id.dpStartDate);
dpEndDate=(DatePicker)findViewById(R.id.dpEndDate);
  Calendar today = Calendar.getInstance();
        dpStartDate.init(today.get(Calendar.YEAR),
                today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH),
                new DatePicker.OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        SmartUtils.ting(MicaAttendanceFilter.this,"Start Date Changes");
                        try {
                            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd M yyyy");
                            Calendar startCalendar = Calendar.getInstance();
                            int startDay=dpStartDate.getDayOfMonth();
                            int startMonth=dpStartDate.getMonth()+1;
                            int startYear=dpStartDate.getYear();
                            String startDate = startDay+" "+startMonth+" "+startYear;
                            startCalendar.setTime(simpleDateFormat.parse(startDate));
                            dpEndDate.setMinDate(startCalendar.getTimeInMillis());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
于 2017-05-04T05:16:45.223 回答