Android显示日期从今天到过去50年

时间:2013-06-07 05:49:06

标签: android android-datepicker android-date

我的DatePickerDailog显示从今天到50年后的日期。但我试图将显示日期从今天改为50年。几次尝试后我失败了,所以我把我的代码放在这里。它应该显示人的出生日期,所以今天没有人可以出生并在计算机上输入。

public class DatePickerDailog extends Dialog {

    private Context Mcontex;

    private int NoOfYear = 50; 
    LayoutParams params_cancel;

    public DatePickerDailog(Context context, Calendar calendar,
            final DatePickerListner dtp) {

        super(context);
        Mcontex = context;

        LinearLayout lytmain = new LinearLayout(Mcontex);
        lytmain.setOrientation(LinearLayout.VERTICAL);
        LinearLayout lytdate = new LinearLayout(Mcontex);
        LinearLayout lytbutton = new LinearLayout(Mcontex);
        lytbutton.setBackgroundResource(R.drawable.bl_button);


        params_cancel= new LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f);


        lytbutton.setBackgroundResource(R.drawable.bl_button);
        lytdate.setBackgroundResource(R.drawable.bl_button);
        lytmain.setBackgroundResource(R.color.white);
        Button btnset = new Button(Mcontex);
        Button btncancel = new Button(Mcontex);

        btncancel.setShadowLayer(2, 1, 1, R.drawable.green_button);
        btnset.setShadowLayer(2, 1, 1, R.drawable.green_button);
        btnset.setBackgroundResource(R.drawable.green_button);
        btncancel.setBackgroundResource(R.drawable.green_button);

        btnset.setTextColor(Mcontex.getResources().getColor(R.color.white));
        btncancel.setTextColor(Mcontex.getResources().getColor(R.color.white));
        btncancel.setTextSize(15);
        btnset.setTextSize(15);
        btnset.setText("R�gler");
        btncancel.setText("D�commander");

        btnset.setGravity(Gravity.CENTER);
        btncancel.setGravity(Gravity.CENTER);


        final WheelView month = new WheelView(Mcontex);
        final WheelView year = new WheelView(Mcontex);
        final WheelView day = new WheelView(Mcontex);

        lytdate.addView(day, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.2f));
        lytdate.addView(month, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f));

        lytdate.addView(year, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        params_cancel.setMargins(2, 2, 4, 2);
        btnset.setLayoutParams(params_cancel);

        lytbutton.addView(btnset, params_cancel);



        lytbutton.addView(btncancel,params_cancel);

        lytbutton.setPadding(5, 5, 5, 5);
        lytmain.addView(lytdate);





        lytmain.addView(lytbutton);





        setContentView(lytmain);

        getWindow().setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                updateDays(year, month, day);

            }
        };

        // month
        int curMonth = calendar.get(Calendar.MONTH);
        String months[] = new String[] {"Janvier", "F�vrier", "Mars",
                "Avril", "Mai", "Juin", "Juillet", "Ao�t", "Septembre",
                "Octobre", "Novembre", "D�cembre" };
        month.setViewAdapter(new DateArrayAdapter(context, months, curMonth));
        month.setCurrentItem(curMonth);
        month.addChangingListener(listener);

        Calendar cal = Calendar.getInstance();
        // year
        int curYear = calendar.get(Calendar.YEAR);
        int Year = cal.get(Calendar.YEAR);

        year.setViewAdapter(new DateNumericAdapter(context, Year ,
                Year + NoOfYear, NoOfYear));
        year.setCurrentItem(Year);
        year.addChangingListener(listener);

        // day
        updateDays(year, month, day);
        day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);

        btnset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar c = updateDays(year, month, day);
                dtp.OnDoneButton(DatePickerDailog.this, c);
            }
        });
        btncancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dtp.OnCancelButton(DatePickerDailog.this);

            }
        });

    }

    Calendar updateDays(WheelView year, WheelView month, WheelView day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,
                calendar.get(Calendar.YEAR) + (year.getCurrentItem()));
        calendar.set(Calendar.MONTH, month.getCurrentItem());

        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        day.setViewAdapter(new DateNumericAdapter(Mcontex, 1, maxDays, calendar
                .get(Calendar.DAY_OF_MONTH) - 1));
        int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
        day.setCurrentItem(curDay - 1, true);
        calendar.set(Calendar.DAY_OF_MONTH, curDay);
        return calendar;

    }

    private class DateNumericAdapter extends NumericWheelAdapter {
        int currentItem;
        int currentValue;

        public DateNumericAdapter(Context context, int minValue, int maxValue,
                int current) {
            super(context, minValue, maxValue);
            this.currentValue = current;
            setTextSize(18);
        }

        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(view.getResources().getColor(R.color.white));
            }
            view.setTypeface(null, Typeface.BOLD);
            view.setTextColor(view.getResources().getColor(R.color.white));
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }

    private class DateArrayAdapter extends ArrayWheelAdapter<String> {
        int currentItem;
        int currentValue;

        public DateArrayAdapter(Context context, String[] items, int current) {
            super(context, items);
            this.currentValue = current;
            setTextSize(18);
        }

        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(view.getResources().getColor(R.color.white));
            }
            view.setTypeface(null, Typeface.BOLD);
            view.setTextColor(view.getResources().getColor(R.color.white));

        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }

    public interface DatePickerListner {
        public void OnDoneButton(Dialog datedialog, Calendar c);

        public void OnCancelButton(Dialog datedialog);
    }
}

更新:在此功能中设置日期

Calendar updateDays(WheelView year, WheelView month, WheelView day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR,
            calendar.get(Calendar.YEAR) + (year.getCurrentItem()));
    calendar.set(Calendar.MONTH, month.getCurrentItem());

    int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    day.setViewAdapter(new DateNumericAdapter(Mcontex, 1, maxDays, calendar
            .get(Calendar.DAY_OF_MONTH) - 1));
    int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
    day.setCurrentItem(curDay - 1, true);
    calendar.set(Calendar.DAY_OF_MONTH, curDay);

    return calendar;

}

1 个答案:

答案 0 :(得分:2)

你可能想说

    year.setViewAdapter(new DateNumericAdapter(context, Year ,
            Year - NoOfYear, NoOfYear));

而不是

    year.setViewAdapter(new DateNumericAdapter(context, Year ,
            Year + NoOfYear, NoOfYear));