单击日期时无法用背景颜色填充圆

时间:2017-01-13 06:54:57

标签: android calendar background-color

我正在开发自定义日历。当用户启动它时,它应显示当前日期,并在其周围绘制圆圈(无背景颜色)。当用户点击日期时,圆圈应填充背景颜色(表示选择日期)。

它不起作用。任何人都可以告诉我的代码可能有什么问题吗?

My GitHub Project
   caldroid project(供参考)

  public class CaldroidSampleActivity extends AppCompatActivity {
        private boolean undo = false;
        private CaldroidFragment caldroidFragment;
        Date selectHalfMoon, selectEvent;
        public ArrayList<String> items;
        ArrayList<String> halfmoon_day, fullmoon_day;
        Calendar itemmonth;
        public Handler handler;
        int prevView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");





            caldroidFragment = new CaldroidSampleCustomFragment();
            CalendarDetail fragobj = new CalendarDetail();


            // If Activity is created after rotation
            if (savedInstanceState != null) {
                caldroidFragment.restoreStatesFromKey(savedInstanceState, "CALDROID_SAVED_STATE");
            }
            // If activity is created from fresh
            else {
                Bundle args = new Bundle();
                Calendar cal = Calendar.getInstance();
                args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
                args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
                args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
                args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, false);


                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, 1);
                Date selectFullMoon = cal.getTime();
                caldroidFragment.setSelectedFullMoonDates(selectFullMoon);

                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -4);
                selectHalfMoon = cal.getTime();
                caldroidFragment.setSelectedHalfMoonDates(selectHalfMoon);
                cal.add(Calendar.DATE, -2);
                selectHalfMoon = cal.getTime();
                caldroidFragment.setSelectedHalfMoonDates(selectHalfMoon);

                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, 3);
                selectEvent = cal.getTime();
                caldroidFragment.setSelectedEvents(selectEvent);

                cal.add(Calendar.DATE, 2);
                selectEvent = cal.getTime();
                caldroidFragment.setSelectedEvents(selectEvent);

                cal.add(Calendar.DATE, 2);
                selectHalfMoon = cal.getTime();
                caldroidFragment.setSelectedEvents(selectHalfMoon);
                cal.add(Calendar.DATE, -4);
                selectEvent = cal.getTime();
                caldroidFragment.setSelectedEvents(selectEvent);
                cal.add(Calendar.DATE, -2);
                selectEvent = cal.getTime();
                caldroidFragment.setSelectedEvents(selectEvent);

                caldroidFragment.refreshView();


                caldroidFragment.setArguments(args);
            }


            FragmentTransaction t = getSupportFragmentManager().beginTransaction();
            t.replace(R.id.calendar1, caldroidFragment);
            t.commit();
      // Setup listener
            final CaldroidListener listener = new CaldroidListener() {


                @Override
                public void onSelectDate(Date date, View view) {

                    Toast.makeText(getApplicationContext(), formatter.format(date),
                            Toast.LENGTH_SHORT).show();

                    String selectedGridDate = formatter.format(date);
                    Log.e("grid val..", "grid val.." + selectedGridDate);
                    String[] separatedTime = selectedGridDate.split("\\s+");

                    CalendarDetail fragobj = new CalendarDetail();
                    Bundle bundle = new Bundle();
                    bundle.putString("edttext", separatedTime[0]);
                    FragmentManager fragmentManager = getFragmentManager();
                    fragobj.setArguments(bundle);
                    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    // CalendarDetail hello = new CalendarDetail();
                    fragmentTransaction.replace(R.id.container, fragobj);
                    fragmentTransaction.commit();

                }


                @Override
                public void onChangeMonth(int month, int year) {

                }

                @Override
                public void onLongClickDate(Date date, View view) {

                }

                @Override
                public void onCaldroidViewCreated() {

                }

            };

            // Setup Caldroid
            caldroidFragment.setCaldroidListener(listener);




          /*  // Customize the calendar
            customizeButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (undo) {
                        customizeButton.setText(getString(R.string.customize));
                        textView.setText("");

                        // Reset calendar
                        caldroidFragment.clearDisableDates();
                        caldroidFragment.clearSelectedDates();
                        caldroidFragment.clearFullMoonDates();
                        caldroidFragment.clearHalfMoonDates();
                        caldroidFragment.clearEventDates();
                        caldroidFragment.setMinDate(null);
                        caldroidFragment.setMaxDate(null);
                        caldroidFragment.setShowNavigationArrows(true);
                        caldroidFragment.setEnableSwipe(true);
                        caldroidFragment.refreshView();
                        undo = false;
                        return;
                    }

                    // Else
                    undo = true;
                    customizeButton.setText(getString(R.string.undo));
                    Calendar cal = Calendar.getInstance();

                    // Min date is last 7 days
                    cal.add(Calendar.DATE, -7);
                    Date minDate = cal.getTime();

                    // Max date is next 7 days
                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 14);
                    Date maxDate = cal.getTime();

                    // Set selected dates
                    // From Date
                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 2);
                    Date fromDate = cal.getTime();

                    // To Date
                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 3);
                    Date toDate = cal.getTime();

                    // Set disabled dates
                    ArrayList<Date> disabledDates = new ArrayList<Date>();
                    for (int i = 5; i < 8; i++) {
                        cal = Calendar.getInstance();
                        cal.add(Calendar.DATE, i);
                        disabledDates.add(cal.getTime());
                    }

                    // Customize
                    caldroidFragment.setMinDate(minDate);
                    caldroidFragment.setMaxDate(maxDate);
                    caldroidFragment.setDisableDates(disabledDates);
                    caldroidFragment.setSelectedDates(fromDate, toDate);
                    caldroidFragment.setShowNavigationArrows(true);
                    caldroidFragment.setEnableSwipe(true);

                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 1);
                    Date selectFullMoon = cal.getTime();
                    fullmoon_day=new ArrayList<String>();
                    fullmoon_day.add("2016-12-27");
                  //  caldroidFragment.setSelectedFullMoonDates(selectFullMoon);
                    caldroidFragment.setFullmoon(fullmoon_day);

                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 2);
                    Date selectHalfMoon = cal.getTime();
                    caldroidFragment.setSelectedHalfMoonDates(selectHalfMoon);

                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, 3);
                    Date selectEvent = cal.getTime();
                    caldroidFragment.setSelectedEvents(selectEvent);

                    caldroidFragment.refreshView();



                    String text = "Today: " + formatter.format(new Date()) + "\n";
                    text += "Min Date: " + formatter.format(minDate) + "\n";
                    text += "Max Date: " + formatter.format(maxDate) + "\n";
                    text += "Select From Date: " + formatter.format(fromDate)
                            + "\n";
                    text += "Select To Date: " + formatter.format(toDate) + "\n";
                    for (Date date : disabledDates) {
                        text += "Disabled Date: " + formatter.format(date) + "\n";
                    }

                    textView.setText(text);
                }
            });*/
        }

        /**
         * Save current states of the Caldroid here
         */
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            // TODO Auto-generated method stub
            super.onSaveInstanceState(outState);

            if (caldroidFragment != null) {
                caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
            }
        }


        public Runnable calendarUpdater = new Runnable() {

            @Override
            public void run() {
                items.clear();
                fullmoon_day.clear();
                halfmoon_day.clear();

                // Print dates of the current week
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                String itemvalue;
                for (int i = 0; i < 7; i++) {
                    itemvalue = df.format(itemmonth.getTime());
                    itemmonth.add(GregorianCalendar.DATE, 1);
                    items.add("2016-12-12");
                    items.add("2016-12-07");
                    items.add("2016-12-15");
                    items.add("2016-12-20");
                    items.add("2016-11-30");
                    items.add("2016-11-28");
                }
                itemvalue = df.format(itemmonth.getTime());
                itemmonth.add(GregorianCalendar.DATE, 1);
                halfmoon_day.add("2016-12-12");
                fullmoon_day.add("2016-12-27");

            }
        };
    }

适配器类:

 public class CaldroidSampleCustomAdapter extends CaldroidGridAdapter {

        public CaldroidSampleCustomAdapter(Context context, int month, int year,
                Map<String, Object> caldroidData,
                Map<String, Object> extraData) {
            super(context, month, year, caldroidData, extraData);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View cellView = convertView;

            // For reuse
            if (convertView == null) {
                cellView = inflater.inflate(R.layout.custom_cell, null);
            }

            int topPadding = cellView.getPaddingTop();
            int leftPadding = cellView.getPaddingLeft();
            int bottomPadding = cellView.getPaddingBottom();
            int rightPadding = cellView.getPaddingRight();

            TextView tv1 = (TextView) cellView.findViewById(R.id.tv1);
            ImageView moon = (ImageView) cellView.findViewById(R.id.moon);
            ImageView fullmoon = (ImageView) cellView.findViewById(R.id.fullmoon);
            ImageView date_icon = (ImageView) cellView.findViewById(R.id.date_icon);

            tv1.setTextColor(Color.BLACK);

            // Get dateTime of this cell
            DateTime dateTime = this.datetimeList.get(position);
            Resources resources = context.getResources();

            // Set color of the dates in previous / next month
            if (dateTime.getMonth() != month) {
                tv1.setTextColor(resources.getColor(R.color.caldroid_darker_gray));
            }

            boolean shouldResetDiabledView = false;
            boolean shouldResetSelectedView = false;

            // Customize for disabled dates and date outside min/max dates
            if ((minDateTime != null && dateTime.lt(minDateTime))
                    || (maxDateTime != null && dateTime.gt(maxDateTime))
                    || (disableDates != null && disableDates.indexOf(dateTime) != -1)) {

                tv1.setTextColor(CaldroidFragment.disabledTextColor);
                if (CaldroidFragment.disabledBackgroundDrawable == -1) {
                    cellView.setBackgroundResource(R.drawable.disable_cell);
                } else {
                    cellView.setBackgroundResource(CaldroidFragment.disabledBackgroundDrawable);
                }

                if (dateTime.equals(getToday())) {
                    cellView.setBackgroundResource(R.drawable.current_date);
                }

            } else {
                shouldResetDiabledView = true;
            }

            // Customize for selected dates
            if (selectedDates != null && selectedDates.indexOf(dateTime) != -1) {
                cellView.setBackgroundColor(resources.getColor(R.color.caldroid_sky_blue));
                tv1.setTextColor(Color.BLACK);

            } else {
                shouldResetSelectedView = true;
            }

            if (selectedFullMoons != null && selectedFullMoons.indexOf(dateTime) != -1) {
                fullmoon.setVisibility(View.VISIBLE);
            } else fullmoon.setVisibility(View.INVISIBLE);

            if (selectedHalfMoons != null && selectedHalfMoons.indexOf(dateTime) != -1) {
                moon.setVisibility(View.VISIBLE);
            } else moon.setVisibility(View.INVISIBLE);

            if (selectedEvents != null && selectedEvents.indexOf(dateTime) != -1) {
                date_icon.setVisibility(View.VISIBLE);
            } else date_icon.setVisibility(View.INVISIBLE);

            if (shouldResetDiabledView && shouldResetSelectedView) {
                // Customize for today
                if (dateTime.equals(getToday())) {
                    cellView.setBackgroundResource(R.drawable.current_date);
                } else {
                    cellView.setBackgroundResource(R.drawable.cell_bg);
                }
            }

            tv1.setText("" + dateTime.getDay());

            // Somehow after setBackgroundResource, the padding collapse.
            // This is to recover the padding
            cellView.setPadding(leftPadding, topPadding, rightPadding,
                    bottomPadding);
   // Set custom color if required
            setCustomResources(dateTime, cellView, tv1);

            return cellView;
        }

    }

1 个答案:

答案 0 :(得分:0)

自定义日历日期选择圆形背景请使用材质日历视图  Here