我检查过多个帖子,但似乎没有人回答我的问题。在Android Dev网站上,我创建了一个日期选择器片段:
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) {
}
}
在我的活动中我只是打电话:
public void showDatePickerDialog(View v){
DialogFragment dateFragment = new DatePickerFragment();
dateFragment.show(getFragmentManager(), "datePicker");
}
如何将用户从选择器中选择的日期传递回活动?基本上我只想将文本字段设置为该日期:
mTextField.set(*The date*);
答案 0 :(得分:1)
我建议您将DatePickerFragment
作为内部课程放在activity
中,当用户填写完日期后,您可以使用{{1}设置onDateSet
。
mTextField
答案 1 :(得分:0)
试试这个:
pickDate = (Button) findViewById(R.id.pick_date_new);
pickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment datePickerFragment = new DialogFragment() {
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
dateLabel.setText(i2 + "/" + (i1 + 1) + "/" + i);
}
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), listener, 1970, 0, 1);
}
};
datePickerFragment.show(getFragmentManager(), "datePicker");
}
});