如何在不使用DatePickerDialog的情况下使用OnDateChangedListener,OnDateSetListener?

时间:2012-11-07 10:55:15

标签: android datepicker datepickerdialog

我希望能够在用户开始更改时检测到datepicker上的更改。但是,我不想使用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 个答案:

答案 0 :(得分:7)

您需要告诉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);
}

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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);
    }
}

答案 3 :(得分: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();
                        }
                    }
                }
        );