我们可以从DateSetListener获取Date of picker Dialog的参考吗?

时间:2011-07-15 10:59:45

标签: android android-2.2-froyo

有没有办法从Date Set Listener获取Date Picker Dialog的引用,因为它的参数只有Date Set Listener的视图?

  if(Type.equalsIgnoreCase("datepicker")
{ mField=new EditText(context); 
((EditText) mField).setText("Pick Date"); 
ll.addView(mField); 
mField.setOnClickListener(new View.OnClickListener()
 {
 @Override public void onClick(View v) 
{
 final Calendar cal = Calendar.getInstance();
 mYear = cal.get(Calendar.YEAR); 
mMonth = cal.get(Calendar.MONTH);
 mDay = cal.get(Calendar.DAY_OF_MONTH); 
DatePickerDialogWithEditText dialog= new DatePickerDialogWithEditText(context, mdatepickerdialog , mYear, mMonth, mDay, (EditText)mField ); 
dialog.show(); } });
 } }
    private DatePickerDialogWithEditText.OnDateSetListener mdatepickerdialog = new DatePickerDialogWithEditText.OnDateSetListener() 
{ 
public void onDateSet(DatePicker view , int year , int month, int date) 
{ 
mYear = year; mMonth = month; mDay = date; EditText et = ((DatePickerDialogWithEditText.OnDateSetListener)this).getAssociatedTextView(); 
et.setText(new StringBuilder().append(mDay).append("-").append(mMonth).append("-").append(mYear)); } };
 } 
    class DatePickerDialogWithEditText extends DatePickerDialog 
{
 EditText tv; 
DatePickerDialogWithEditText(Context c, OnDateSetListener oD, int y, int m, int d, EditText tv) 
{ 
super(c, oD, y, m, d); this.tv = tv;
 }
 EditText getAssociatedTextView() 
{ 
return tv; 
} 
} 

2 个答案:

答案 0 :(得分:1)

DatePickerDialogWithEditText dialog= new DatePickerDialogWithEditText(context, mdatepickerdialog , mYear, mMonth, mDay, (EditText)mField ); 

以上陈述的实例 DatePickerDialogWithEditText对话框该变量类变量或实例变量,以便它可以访问整个类.... 我的意思是这样的

Class A
{
private DatePickerDialogWithEditText dialog;
public some method
{
dialog= new DatePickerDialogWithEditText(context, mdatepickerdialog , mYear, mMonth, mDay, (EditText)mField ); 
}
}

答案 1 :(得分:1)

这是代码:

if(Type.equalsIgnoreCase("datepicker"))
        {
            mField=new EditText(context);
            ((EditText) mField).setText("Pick Date");
            ll.addView(mField);
            mField.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                    dialognew = new Mynewdialog(context);
                    dialognew.dialog_obj((EditText) mField);
                }
            });
        }
    }   

    protected DatePickerDialogWithEditText.OnDateSetListener mdatepickerdialog = new DatePickerDialogWithEditText.OnDateSetListener()
    {
        public void onDateSet(DatePicker view , int year , int month, int date)
         {
             mYear = year;
             mMonth = month;
             mDay = date;
             dialognew.showdialogtext();

         }
     };
}

class DatePickerDialogWithEditText extends DatePickerDialog 
{
    EditText tv;    

    DatePickerDialogWithEditText(Context c, OnDateSetListener oD, int y, int m, int d, EditText tv) 
    {
        super(c, oD, y, m, d);
        this.tv = tv;
    }

    EditText getAssociatedTextView() 
    {
        return tv;
    }
}


class Mynewdialog 
{
    private Context context;
    Display dis = new Display(context);
    private DatePickerDialogWithEditText dialog;

    public Mynewdialog(Context mcontext)
    {
        context=mcontext;       
    }

    public void dialog_obj(EditText editTextView)
    {
        final Calendar cal = Calendar.getInstance();
        dis.mYear = cal.get(Calendar.YEAR);
        dis.mMonth = cal.get(Calendar.MONTH);
        dis.mDay = cal.get(Calendar.DAY_OF_MONTH);
        dialog = new DatePickerDialogWithEditText(context, dis.mdatepickerdialog, dis.mYear, dis.mMonth, dis.mDay, editTextView);
        dialog.show();
    }

    public void showdialogtext()
    {
        EditText et = dialog.getAssociatedTextView();
        et.setText(new StringBuilder().append(dis.mDay).append("-").append(dis.mMonth).append("-").append(dis.mYear));
    }   
}