我有一个 Fragment,我有一个 FragmentTabHost 和一些附加到它的选项卡。每个选项卡也是一个 Fragment。现在,我想从其中任何一个中显示 DatePicker 和 Timepicker,那么我将如何使用 DialogFragment 或任何其他方法显示它们?
问问题
1329 次
1 回答
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);
}
}
于 2013-12-19T09:44:30.387 回答