如何创建日期微调器,它将EditText中的当前日期显示为Spinner中的默认日期和未来日期(例如,接下来的30天)
我在许多应用程序中使用了日期选择器,所以我熟悉日期选择器对话框,但对日期微调器没有任何想法.
注意
请不要告诉我如何将EditText设置为Spinner的样式
编辑:1 @erakitin推荐
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_date); Spinner spinnerDateIn = (Spinner) findVIEwByID(R.ID.spinnerDateIn); Spinner spinnerDateOut = (Spinner) findVIEwByID(R.ID.spinnerDateOut); spinnerDateIn.setAdapter(new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30)); }
编辑:2 @erakitin在新的更新中推荐
CalendarSpinnerAdapter mSpinnerDateInAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30); spinnerDate.setAdapter(mSpinnerDateInAdapter); spinnerDate.setonItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() { @OverrIDe public voID onItemSelected(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { spinnerDate.setSelection(position); } @OverrIDe public voID onnothingSelected(AdapterVIEw<?> parent) { } });
现在,尝试获取String中所选Item的值,但得到:java.util.GregorianCalendar,我在哪里做错了?
strDate = spinnerDate.getSelectedItem().toString();
解决方法:
下面有一个适用于Spinner的简单适配器示例.
public class CalendarSpinnerAdapter extends BaseAdapter { private SimpleDateFormat mDateFormat = new SimpleDateFormat("d MMM yyyy"); private LayoutInflater mInflater; private Calendar mCalendar; private int mDayCount; private int mLastRequestedDay = 0; public CalendarSpinnerAdapter(Context context, int dayCount) { mInflater = LayoutInflater.from(context); mDayCount = dayCount; mCalendar = Calendar.getInstance(); } @OverrIDe public int getCount() { return mDayCount; } @OverrIDe public Calendar getItem(int position) { mCalendar.add(Calendar.DAY_OF_YEAR, position - mLastRequestedDay); mLastRequestedDay = position; return mCalendar; } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { if (convertVIEw == null) { convertVIEw = mInflater.inflate(androID.R.layout.simple_spinner_dropdown_item, parent, false); } Calendar item = getItem(position); ((TextVIEw) convertVIEw).setText(mDateFormat.format(item.getTimeInMillis())); return convertVIEw; } @OverrIDe public VIEw getDropDownVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { return getVIEw(position, convertVIEw, parent); }}
如何使用它来显示当前日期和接下来的29天:
mDateSpinner.setAdapter(new CalendarSpinnerAdapter(getActivity(), 30));
UPD:
我们应该在adapter的构造函数中添加一个参数来设置开始日期:
public CalendarSpinnerAdapter(Context context, Calendar startDate, int dayCount) { mInflater = LayoutInflater.from(context); mDayCount = dayCount; mCalendar = Calendar.getInstance(); mCalendar.setTimeInMillis(startDate.getTimeInMillis());}
然后为spinnerDateIn添加监听器,我们可以在其中初始化第二个微调器:
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_date); Spinner spinnerDateIn = (Spinner) findVIEwByID(R.ID.spinnerDateIn); final Spinner spinnerDateOut = (Spinner) findVIEwByID(R.ID.spinnerDateOut); mSpinnerDateInAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, Calendar.getInstance(), 30); spinnerDateIn.setAdapter(mSpinnerDateInAdapter); spinnerDateIn.setonItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() { @OverrIDe public voID onItemSelected(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { Calendar dateIn = Calendar.getInstance(); dateIn.setTimeInMillis(mSpinnerDateInAdapter.getItem(position).getTimeInMillis()); dateIn.add(Calendar.DAY_OF_YEAR, 1); // add one day mSpinnerDateOutAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, dateIn, 30); spinnerDateOut.setAdapter(mSpinnerDateOutAdapter); } @OverrIDe public voID onnothingSelected(AdapterVIEw<?> parent) { } });}
我没有尝试这个代码,但它应该工作.
UPD2:
spinnerDate.setSelection(position)返回Calendar类实例.如果要将选定日期作为String,则应格式化它.尝试使用以下方法:
private String getSelectedDateAsstring(Spinner dateSpinner) { Calendar selectedDate = (Calendar) dateSpinner.getSelectedItem(); return new SimpleDateFormat("d MMM yyyy").format(selectedDate.getTimeInMillis());}
总结 以上是内存溢出为你收集整理的java – 如何创建Spinner以显示当前和接下来的30个日期全部内容,希望文章能够帮你解决java – 如何创建Spinner以显示当前和接下来的30个日期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)