我正在按照此链接创建一个 DatePickerDialog 并将所选日期复制到 Editext 字段。但是,我无法理解如何做到这一点,因为无论我在 onDateSet 方法中做什么,它都无法识别。如果有人好心帮忙。
主要 XML:
<EditText
android:id="@+id/add_datepurchased"
android:inputType="date"
android:onClick="showdate" />
主类:
EditText datpurchased;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_entry);
// Show the Up button in the action bar.
setupActionBar();
datpurchased = (EditText) findViewById(R.id.add_datepurchased);
}
public void showdate(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
DatePickerFragment 类:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day){
EditText datpurchased = (EditText) findViewById(R.id.add_datepurchased); // GIVES ERROR
datpurchased.setText(year+"-"+(month+1)+"-"+day);
}
}