[英]Trying to set text of EditText from DatePicker DialogFragment
我只是想从我在网上找到的许多android datepicker教程之一创建的datepicker对话框中获取日期。 一旦选择了文本日期,我似乎在实际检索文本日期的某处出错。 我有一个DatePickerFragment类,该类从嵌套在MainActivity活动类内的CreateEvent片段调用。 这是DatePicker:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private EditText txtDate;
@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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
txtDate = (EditText)view.findViewById(R.id.txtDate);
txtDate.setText(day + " " + (month + 1) + " " + year);
}
}
当我实际尝试设置日期时,我尝试Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference
,这意味着它实际上无法找到edittext元素在我的创建事件片段xml文件中。
我应该如何从onDateSet
设置此edittext元素的文本,还是必须更改方法?
我以这种方式尝试过,但无济于事。
我是通过以下代码行完成此操作的:
public class DatePickerDialogMy extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DateSetting dateSetting=new DateSetting(getActivity());
Calendar calendar= Calendar.getInstance();
int year= calendar.get(calendar.YEAR);
int month=calendar.get(calendar.MONTH);
int day=calendar.get(calendar.DAY_OF_MONTH);
DatePickerDialog dialog;
dialog=new DatePickerDialog(getActivity(),dateSetting,year,month,day);
return dialog;
}
}
二等舱:
public class DateSetting implements android.app.DatePickerDialog.OnDateSetListener {
Context context;
public DateSetting(Context context){
this.context=context;
}
@Override
public void onDateSet(DatePicker view, int year, int dateSetting, int dayOfMonth) {
Toast.makeText(context, "selected date:" + dateSetting + "/" + dayOfMonth + "/" + year, Toast.LENGTH_LONG).show();
// MainActivity.test.setText(String.valueOf(dateSetting));
MyActivity.dobEditText.setText(dateSetting+"/"+dayOfMonth+ "/" + year);
}
}
通话方式:
DatePickerDialogMy datePickerDialog = new DatePickerDialogMy();
datePickerDialog.show(getSupportFragmentManager(), "date_picker");
您需要使用一个接口来将数据从datepicker
到调用者fragment
:
public interface DateListener {
void passDate(String date);
}
创建一个名为mListener
的成员变量 :
private DateListener mListener;
覆盖onAttach
和onDetach
fragment
方法:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (DateListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement DateListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
接下来,在调用程序fragment
实现此interface
,并覆盖passDate
方法:
@Override
public void passDate(String date) {
// Do something with 'date', yourEditText.setText(date) for the example
}
而且您应该很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.