我想DatePickerDialog
在 Android 活动本身中嵌入一个。我不希望DatePicker
在单击按钮作为对话框时出现,而是将其嵌入到我的活动本身中。我曾尝试DatePicker
在布局中使用视图,但这似乎与DatePickerDialog
.
我DialogFragment
用于创建和DatePickerDialog
使用以下代码onCreate
MainAcitivity.java
FragmentTransaction ft = getFragmentManager().beginTransaction();
DatePickerDialogFragment datePickerDialogFragment = DatePickerDialogFragment.newInstance();
ft.add(R.id.main_screen_layout, datePickerDialogFragment); // main_screen_layout is given as the id for the layout related to MainActivity
ft.commit();
// DatePickerDialogFragment.java
public class DatePickerDialogFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public static DatePickerDialogFragment newInstance() {
return new DatePickerDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_datepicker, container, false);
View tv = v.findViewById(R.id.tv);
((TextView)tv).setText("This is a new instance");
return v;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
return new DatePickerDialog(getActivity(), this,
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
}
}
如何解决这个问题?