我正在尝试使用日期选择器将用户输入反馈回某些文本字段。我正在使用Date选择器对象和监听器。我收到一条错误,指出'无法找到符号变量MyListener。在线:
DatePickerFragment datePicker = new DatePickerFragment(MyListener);
如果我用'this'替换'MyListner',我会收到此消息
“错误:类DatePickerFragment中的构造函数DatePickerFragment 不能适用于给定的类型;必需:NewClutch.MyListener 发现:NewClutch原因:实际参数NewClutch无法转换 通过方法调用转换“
到NewClutch.MyListener
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private MyListener listener;
public DatePickerFragment(MyListener listener){
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
int selectedDate;
View targetView;
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
selectedDate = day+month+year;
if(listener!=null){
listener.theDateHasChanged(selectedDate, targetView);
}
}
public int getSelectedDate() {
return selectedDate;
}
public int setTargetBox(View view) {
targetView = view;
}
}
/* close datePickerFragment class */
public void showDatePickerDialog(View view ) {
DatePickerFragment datePicker = new DatePickerFragment(this);
datePicker.setTargetBox(view);
datePicker.show(getFragmentManager(), "datePicker");
//int output = ((DatePickerFragment)datePicker).getSelectedDate();
}
public class MyListener {
public void theDateHasChanged(int selectedDate, View targetView) {
if(targetView.getId() == R.id.clutch_date_laid_box)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.date_two_box);
// -- change text size --
txtView.setText(selectedDate);
return;
}
if(targetView.getId() == R.id.clutch_name_box)
{
// --- find the text view --
TextView txtView = (TextView) findViewById(R.id.date_one_box);
// -- change text size --
txtView.setText(selectedDate);
return;
}
}
}