我在
Android Fragments上是新的,我想知道如何在简单的Fragmentand中显示或实现DatePicker而不是FragmentActivity.
例如我的班级名称是:
@H_502_4@public class FragmentAddCard extends Fragment { }谢谢.
解决方法
在你的按钮中,点击这样调用DateFragment
@H_502_4@dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(),"DatePicker");
}
});
以下是DateFragment的代码片段
@H_502_4@public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); int yy = calendar.get(Calendar.YEAR); int mm = calendar.get(Calendar.MONTH); int dd = calendar.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(),this,yy,mm,dd); } public void onDateSet(DatePicker view,int yy,int mm,int dd) { populateSetDate(yy,mm+1,dd); } public void populateSetDate(int year,int month,int day) { dob.setText(month+"/"+day+"/"+year); } }