我有一个Fragment,我有一个FragmentTabHost并附有一些标签。每个标签也是一个Fragment.Now,从其中任何一个,我想显示DatePicker和Timepicker,那么我将如何使用DialogFragment或任何其他方法显示它们?
答案 0 :(得分:0)
试试这段代码..
public class student extends Fragment
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view= inflater.inflate(R.layout.layout_main, container, false);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
editdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker();
}
});
edittime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showTimePicker();
}
});
}
private void showDatePicker() {
DatePickerFragment date = new DatePickerFragment();
/**
* Set Up Current Date Into dialog
*/
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}
OnDateSetListener ondate = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear+1)
+ "-" + String.valueOf(year));
}
};
private void showTimePicker()
{
//DatePickerFragment date = new DatePickerFragment();
TimePickerFragment time= new TimePickerFragment();
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("hour", calender.HOUR_OF_DAY);
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("minute", calender.get(Calendar.MINUTE));
time.setArguments(args);
time.setCallBack(ontime);
time.show(getFragmentManager(), "Time Picker");
}
OnTimeSetListener ontime = new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
txt_time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
}
};
}
创建两个类DatePickerFragment和TimePickerFragment
public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
public DatePickerFragment() {
}
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
private int year, month, day;
@SuppressLint("NewApi")
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
}
TimePickerFragment.java
public class TimePickerFragment extends DialogFragment {
OnTimeSetListener onTimeSet;
public TimePickerFragment() {
}
public void setCallBack(OnTimeSetListener ontime) {
onTimeSet = ontime;
}
@SuppressLint("NewApi")
private int hour, minute;
public void setArguments(Bundle args) {
super.setArguments(args);
hour = args.getInt("hour");
minute = args.getInt("minute");
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(getActivity(), onTimeSet, hour, minute, false);
}
}