日期选择器对话框在片段中不起作用[关闭]
Posted
技术标签:
【中文标题】日期选择器对话框在片段中不起作用[关闭]【英文标题】:Date picker dialog is not working in fragment [closed] 【发布时间】:2014-01-14 19:49:15 【问题描述】:在这里我为日期选择器制作了一个简单的代码,它适用于当前日期、年份和日期,它不适用于片段
public class Activity extends Sherlockfragment
EditText dob;
public static final int DATE_DIALOG_ID = 1009;
String dob_string;
int year = 2013, month = 10, day = 1, dayofmonth = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.update_profile, container, false);
mdob = (EditText)view. findViewById(R.id.dob);
mdob.setOnClickListener(new View.OnClickListener()
@SuppressWarnings("deprecation")
public void onClick(View v)
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
);
return view;
// ****************************#DATE PICKER DIALOG# **************************
@Override
protected Dialog onCreateDialog(int id)
switch (id)
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
return null;
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay)
year = selectedYear;
month = selectedMonth + 1;
day = selectedDay;
Calendar cal = Calendar.getInstance();
cal.set(selectedYear, selectedMonth, selectedDay);
DateFormatSymbols dfs = new DateFormatSymbols(Locale.getDefault());
String weekdays[] = dfs.getWeekdays();
int daykk = cal.get(Calendar.DAY_OF_WEEK);
String dayof = weekdays[daykk];
mdob.setText(day + "-" + month + "-" + year + dayof);
mdob_string = day + "-" + month + "-" + year;
;
【问题讨论】:
到底发生了什么?你到底想要什么? 你有问题要问吗? 不工作意味着什么.. public final boolean showDialog (int id, Bundle args) 在 API 级别 8 中添加此方法在 API 级别 13 中已弃用。将新的 DialogFragment 类与 FragmentManager 一起使用;这也可以通过 android 兼容性包在旧平台上使用。 @PiyushGupta 它不工作所以你能提供我任何参考 【参考方案1】:public class Activity extends Sherlockfragment
static EditText mdob;
static String dob_string;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.update_profile, container, false);
mdob = (EditText)view. findViewById(R.id.dob);
mdob.setOnClickListener(new View.OnClickListener()
@SuppressWarnings("deprecation")
public void onClick(View v)
// TODO Auto-generated method stub
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
);
return view;
// *********************************CLASS****************************************************************************
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener
int year = 2013, month = 10, day = 1, dayofmonth = 1;
public Dialog onCreateDialog(Bundle savedInstanceState)
// Use the current date as the default date in the picker
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay)
// Do something with the date chosen by the user
year = selectedYear;
month = selectedMonth + 1;
day = selectedDay;
Calendar cal = Calendar.getInstance();
cal.set(selectedYear, selectedMonth, selectedDay);
DateFormatSymbols dfs = new DateFormatSymbols(Locale.getDefault());
String weekdays[] = dfs.getWeekdays();
int daykk = cal.get(Calendar.DAY_OF_WEEK);
String dayof = weekdays[daykk];
Activity.mdob.setText(day + "-" + month + "-" + year + dayof);
Activity.mdob_string = day + "-" + month + "-" + year;
【讨论】:
它工作正常......【参考方案2】:您可以为此创建一个自定义类,如下所示:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
public void onDateSet(DatePicker view, int year, int month, int day)
// Do something with the date chosen by the user
参考:Pickers with Fragment。
【讨论】:
以上是关于日期选择器对话框在片段中不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
jquery ui对话框中的基础日期选择器在Internet Explorer中不起作用
禁用日期的 HTML5 日期选择器在 Angular 中不起作用