日期&使用android微调器的时间

时间:2012-08-20 13:34:46

标签: android xml user-interface

我希望复制android开发者应用教程中显示的功能。

我已经实现了正确的布局,但任何人都可以帮助我让微调器能够选择日期和时间。除非它是硬编码的,但我假设在android中内置了一些日期/时间设施。

有什么想法吗? enter image description here

2 个答案:

答案 0 :(得分:2)

  

任何人都可以帮助我让旋转器能够选择日期和时间。

使用android api中提供的Pickers将为您提供帮助用户选择日期和时间所需的小部件。

答案 1 :(得分:0)

我使用了Joda datetime。另外,我在微调器中创建了自己的项目视图 - R.layout.spinner_item

public class YourFragment extends Fragment {

    private DateSpinnerAdapter daysAdapt;
    private DateSpinnerAdapter monthsAdapt;
    private DateSpinnerAdapter yearsAdapt;

    private static final int MIN_YEAR = 1935;
    private static final int YEARS_RANGE = 61;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initSpinners();
    }

    private void initSpinners() {
        continueButton.setOnClickListener(this);

        monthsAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
        monthsAdapt.setData(getActivity().getResources().getStringArray(R.array.monthsArray));
        monthSpinner.setAdapter(monthsAdapt);
        setMonthsListener();

        yearsAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
        yearsAdapt.setData(getYears());
        yearsSpinner.setAdapter(yearsAdapt);
        setYearsListener();

        daysAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
        setDaysSpinnerValues(monthSpinner.getSelectedItemPosition(), yearsSpinner.getSelectedItemPosition());
        daysSpinner.setAdapter(daysAdapt);
    }

    private void setMonthsListener() {
        monthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int number, long arg3) {
                setDaysSpinnerValues(number, yearsSpinner.getSelectedItemPosition() + MIN_YEAR);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }

    private  void setYearsListener() {
        yearsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int number, long arg3) {
                setDaysSpinnerValues(monthSpinner.getSelectedItemPosition(), number + MIN_YEAR);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }

    private int getMonthOfBirth() {
        return monthSpinner.getSelectedItemPosition() + 1;
    }

    private int getYearOfBirth() {
        return yearsSpinner.getSelectedItemPosition() + MIN_YEAR;
    }

    private int getDayOfBirth() {
        return daysSpinner.getSelectedItemPosition() + 1;
    }

    private DateTime getBirthday() {
        return DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(getStringDate());
    }

    private String getStringDate() {
        return getYearOfBirth() + "-" + getMonthOfBirth() + "-" + getDayOfBirth();
    }

    private String[] getYears() {
        String[] years = new String[YEARS_RANGE];
        for(int i = 0; i < YEARS_RANGE; i++) {
            years[i] = "" + (i + MIN_YEAR);
        }
        return years;
    }

    private String[] getDays(int month, int year) {
        int maxDays = getDaysInMonth(month, year);
        String[] days = new String[maxDays];
        for(int i = 0; i < maxDays; i++) {
            days[i] = "" + (i + 1);
        }
        return days;
    }

    public int getDaysInMonth(int month, int year) {
        Calendar cal = new GregorianCalendar();
        cal.set(year, month, 1); //note that the month in Calendar goes from 0-11
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public void setDaysSpinnerValues(int month, int year) {
        daysAdapt.clear();
        daysAdapt.setData(getDays(month, year));
        daysAdapt.notifyDataSetChanged();
    }
}

和适配器:

public class DateSpinnerAdapter extends ArrayAdapter<String>{

    protected FragmentActivity context;

    public DateSpinnerAdapter(FragmentActivity context, int resource) {
        super(context, resource);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View element = super.getView(position, convertView, parent);
        ((TextView) element).setTextColor(context.getResources().getColor(R.color.white));
        LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        element.setLayoutParams(layoutParams);
        return element;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View element =super.getDropDownView(position, convertView, parent);
        element.setPadding(15, 30, 0, 30);
        return element;
    }

    @TargetApi(11)
    public void setData(String[] data) {
        clear();
        if (data != null) {
            //If the platform supports it, use addAll, otherwise add in loop
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                addAll(data);
            }else{
                for(String item: data){
                    add(item);
                }
            }
        }
    }
}