[英]Trying to set text of EditText from DatePicker DialogFragment
I am simply trying to get the date from a datepicker dialog I created from one of the many android datepicker tutorials I found online. 我只是想从我在网上找到的许多android datepicker教程之一创建的datepicker对话框中获取日期。 I seem to be going wrong somewhere in the actual retrieving of the text date once it is selected. 一旦选择了文本日期,我似乎在实际检索文本日期的某处出错。 I have a DatePickerFragment class, which is called from a CreateEvent fragment which is nested inside my MainActivity activity class. 我有一个DatePickerFragment类,该类从嵌套在MainActivity活动类内的CreateEvent片段调用。 This is the DatePicker: 这是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);
}
}
I get Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference
when I actually try and set the date, which I understand means it can't actually find the edittext element in my create event fragment xml file. 当我实际尝试设置日期时,我尝试Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference
,这意味着它实际上无法找到edittext元素在我的创建事件片段xml文件中。
How should I go about setting the text of this edittext element from onDateSet
or do I have to change my approach? 我应该如何从onDateSet
设置此edittext元素的文本,还是必须更改方法?
I have done this by below lines of code: 我是通过以下代码行完成此操作的:
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;
}
}
Second Class: 二等舱:
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);
}
}
How to call: 通话方式:
DatePickerDialogMy datePickerDialog = new DatePickerDialogMy();
datePickerDialog.show(getSupportFragmentManager(), "date_picker");
You need to use an interface to get the data from the datepicker
to the caller fragment
: 您需要使用一个接口来将数据从datepicker
到调用者fragment
:
public interface DateListener {
void passDate(String date);
}
Create a member variable called mListener
: 创建一个名为mListener
的成员变量 :
private DateListener mListener;
Override the onAttach
& onDetach
fragment
methods: 覆盖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;
}
Next, implement this interface
in the caller fragment
and override the passDate
method: 接下来,在调用程序fragment
实现此interface
,并覆盖passDate
方法:
@Override
public void passDate(String date) {
// Do something with 'date', yourEditText.setText(date) for the example
}
And you should be good to go. 而且您应该很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.