我希望能够在用户开始更改时检测到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 arg3) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext()," thiv view"+arg0,"ok"); }
}
解决方法
您需要告诉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); }