2012-01-18 101 views
1

我在我的Activity类中有4个DatePicker对话框,它们在单击4个按钮时显示。现在对于这些datePickerDialog我想使用相同的侦听器。
多个日期选择器对话框

 DatePickerDialog taxPickerDialog = new DatePickerDialog(getCurrentContext(),dateSetListener,calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)); 
     taxPickerDialog.show(); 

     DatePickerDialog fcPickerDialog = new DatePickerDialog(getCurrentContext(),dateSetListener,calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)); 
     fcPickerDialog.show(); 
     break; 

     DatePickerDialog expiryPickerDialog = new DatePickerDialog(getCurrentContext(),dateSetListener,calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)); 
     expiryPickerDialog.show(); 


     DatePickerDialog permitPickerDialog = new DatePickerDialog(getCurrentContext(),dateSetListener,calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)); 
     permitPickerDialog.show(); 

现在我想使用监听器,但dnot知道如何实现这个???

DatePickerDialog.OnDateSetListener dateSetListener =new DatePickerDialog.OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
    switch(????){????} 
} 
}; 
+0

那么这个你可能会喜欢看 http://stackoverflow.com/questions/3734981/multiple-datepickers-in-same-activity – 2012-08-31 04:53:37

+0

也许这种方法可能对您有用: HTTP: //stackoverflow.com/questions/3734981/multiple-datepickers-in-same-activity – Lara 2012-09-10 08:38:57

回答

0
class DPlistener implements OnDateSetListener 
{ 

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

     //you can here 
     if (view.equals(expiryPickerDialog)) 
     { 
     //do needfull 
       } 
    }} 

并注册这个监听器在日期选择器的意见,确保这个类是内部类的活动

+0

不工作。在调试时...如果条件返回false。 – 2012-01-18 08:08:19

0

希望你现在可能已经解决了这一点。只是提出一个想法 - “您可以设置某种IDTAGDatePicker与每个Dialog相关联,然后您可以通过查询您设置的(ID/TAG)来区分。”

+0

感谢您的回复..但这个问题仍然存在。我不能使用ID,因为DatePicker是在Java代码上创建的,而不是在Xml上创建的, – 2012-07-17 12:06:24

+0

Mohit,您可以使用View的setId()方法在代码中添加ID。 – iuq 2012-07-18 06:58:33

0

我知道它是一个很晚的答复,但我最近经历了相同的情况。因此,这里是我的回答,对片段本身

`DialogFragment.Show(FragmentManager manager, String tag))` 

方法设置标签。所以这种标签可以使用如下:

Fragment dateTo=getActivity().getSupportFragmentManager().findFragmentByTag("dateTo"); 
      if(dateTo!=null){ 
       //do something 

      } 

一个用例是可以显示更多然后在活动一个Fragment,并确定你可以找到与给定tag.If它是片段的片段不为空, 答对了!!!

相关问题