[转帖]Android 自定义日历控件_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4003 | 回复: 0   主题: [转帖]Android 自定义日历控件        下一篇 
jie.liang
注册用户
等级:少校
经验:1003
发帖:77
精华:0
注册:2013-10-11
状态:离线
发送短消息息给jie.liang 加好友    发送短消息息给jie.liang 发消息
发表于: IP:您无权察看 2013-10-18 14:07:29 | [全部帖] [楼主帖] 楼主

有图有真像:

北京联动北方科技有限公司

日历控件View:

[java]

  1. /** 
  2.  * 日历控件 功能:获得点选的日期区间 
  3.  * 
  4.  */ 
  5. public class CalendarView extends View implements View.OnTouchListener { 
  6.        private final static String TAG = "anCalendar"; 
  7.        private Date selectedStartDate; 
  8.        private Date selectedEndDate; 
  9.        private Date curDate; // 当前日历显示的月 
  10.        private Date today; // 今天的日期文字显示红色 
  11.        private Date downDate; // 手指按下状态时临时日期 
  12.        private Date showFirstDate, showLastDate; // 日历显示的第一个日期和最后一个日期 
  13.        private int downIndex; // 按下的格子索引 
  14.        private Calendar calendar; 
  15.        private Surface surface; 
  16.        private int[] date = new int[42]; // 日历显示数字 
  17.        private int curStartIndex, curEndIndex; // 当前显示的日历起始的索引 
  18.        //private boolean completed = false; // 为false表示只选择了开始日期,true表示结束日期也选择了 
  19.        //给控件设置监听事件 
  20.        private OnItemClickListener onItemClickListener; 
  21.       
  22.        public CalendarView(Context context) { 
  23.              super(context); 
  24.              init(); 
  25.        } 
  26.       
  27.        public CalendarView(Context context, AttributeSet attrs) { 
  28.              super(context, attrs); 
  29.              init(); 
  30.        } 
  31.       
  32.        private void init() { 
  33.              curDate = selectedStartDate = selectedEndDate = today = new Date(); 
  34.              calendar = Calendar.getInstance(); 
  35.              calendar.setTime(curDate); 
  36.              surface = new Surface(); 
  37.              surface.density = getResources().getDisplayMetrics().density; 
  38.              setBackgroundColor(surface.bgColor); 
  39.              setOnTouchListener(this); 
  40.        } 
  41.       
  42.        @Override 
  43.        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  44.              surface.width = getResources().getDisplayMetrics().widthPixels; 
  45.              surface.height = (int) (getResources().getDisplayMetrics().heightPixels*2/5); 
  46.             // if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.EXACTLY) { 
  47.                   // surface.width = View.MeasureSpec.getSize(widthMeasureSpec); 
  48.             // } 
  49.             // if (View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec.EXACTLY) { 
  50.                   // surface.height = View.MeasureSpec.getSize(heightMeasureSpec); 
  51.             // } 
  52.              widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.width, 
  53.              View.MeasureSpec.EXACTLY); 
  54.              heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.height, 
  55.              View.MeasureSpec.EXACTLY); 
  56.              setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
  57.              super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  58.        } 
  59.       
  60.        @Override 
  61.        protected void onLayout(boolean changed, int left, int top, int right, 
  62.        int bottom) { 
  63.              Log.d(TAG, "[onLayout] changed:" 
  64.              + (changed ? "new size" : "not change") + " left:" + left 
  65.              + " top:" + top + " right:" + right + " bottom:" + bottom); 
  66.              if (changed) { 
  67.                    surface.init(); 
  68.              } 
  69.              super.onLayout(changed, left, top, right, bottom); 
  70.        } 
  71.       
  72.        @Override 
  73.        protected void onDraw(Canvas canvas) { 
  74.              Log.d(TAG, "onDraw"); 
  75.              // 画框 
  76.              canvas.drawPath(surface.boxPath, surface.borderPaint); 
  77.              // 年月 
  78.              //String monthText = getYearAndmonth(); 
  79.              //float textWidth = surface.monthPaint.measureText(monthText); 
  80.              //canvas.drawText(monthText, (surface.width - textWidth) / 2f, 
  81.              // surface.monthHeight * 3 / 4f, surface.monthPaint); 
  82.              // 上一月/下一月 
  83.              //canvas.drawPath(surface.preMonthBtnPath, surface.monthChangeBtnPaint); 
  84.              //canvas.drawPath(surface.nextMonthBtnPath, surface.monthChangeBtnPaint); 
  85.              // 星期 
  86.              float weekTextY = surface.monthHeight + surface.weekHeight * 3 / 4f; 
  87.              // 星期背景 
  88.             // surface.cellBgPaint.setColor(surface.textColor); 
  89.             // canvas.drawRect(surface.weekHeight, surface.width, surface.weekHeight, surface.width, surface.cellBgPaint); 
  90.              for (int i = 0; i < surface.weekText.length; i++) { 
  91.                    float weekTextX = i 
  92.                    * surface.cellWidth 
  93.                    + (surface.cellWidth - surface.weekPaint 
  94.                    .measureText(surface.weekText[i])) / 2f; 
  95.                    canvas.drawText(surface.weekText[i], weekTextX, weekTextY, 
  96.                    surface.weekPaint); 
  97.              } 
  98.             
  99.              // 计算日期 
  100.              calculateDate(); 
  101.              // 按下状态,选择状态背景色 
  102.              drawDownOrSelectedBg(canvas); 
  103.              // write date number 
  104.              // today index 
  105.              int todayIndex = -1; 
  106.              calendar.setTime(curDate); 
  107.              String curYearAndMonth = calendar.get(Calendar.YEAR) + "" 
  108.              + calendar.get(Calendar.MONTH); 
  109.              calendar.setTime(today); 
  110.              String todayYearAndMonth = calendar.get(Calendar.YEAR) + "" 
  111.              + calendar.get(Calendar.MONTH); 
  112.              if (curYearAndMonth.equals(todayYearAndMonth)) { 
  113.                    int todayNumber = calendar.get(Calendar.DAY_OF_MONTH); 
  114.                    todayIndex = curStartIndex + todayNumber - 1; 
  115.              } 
  116.              for (int i = 0; i < 42; i++) { 
  117.                    int color = surface.textColor; 
  118.                    if (isLastMonth(i)) { 
  119.                          color = surface.borderColor; 
  120.                    } else if (isNextMonth(i)) { 
  121.                          color = surface.borderColor; 
  122.                    } 
  123.                    if (todayIndex != -1 && i == todayIndex) { 
  124.                          color = surface.todayNumberColor; 
  125.                    } 
  126.                    drawCellText(canvas, i, date[i] + "", color); 
  127.              } 
  128.              super.onDraw(canvas); 
  129.        } 
  130.       
  131.        private void calculateDate() { 
  132.              calendar.setTime(curDate); 
  133.              calendar.set(Calendar.DAY_OF_MONTH, 1); 
  134.              int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK); 
  135.              Log.d(TAG, "day in week:" + dayInWeek); 
  136.              int monthStart = dayInWeek; 
  137.              if (monthStart == 1) { 
  138.                    monthStart = 8; 
  139.              } 
  140.              monthStart -= 1; //以日为开头-1,以星期一为开头-2 
  141.              curStartIndex = monthStart; 
  142.              date[monthStart] = 1; 
  143.              // last month 
  144.              if (monthStart > 0) { 
  145.                    calendar.set(Calendar.DAY_OF_MONTH, 0); 
  146.                    int dayInmonth = calendar.get(Calendar.DAY_OF_MONTH); 
  147.                    for (int i = monthStart - 1; i >= 0; i--) { 
  148.                          date[i] = dayInmonth; 
  149.                          dayInmonth--; 
  150.                    } 
  151.                    calendar.set(Calendar.DAY_OF_MONTH, date[0]); 
  152.              } 
  153.              showFirstDate = calendar.getTime(); 
  154.              // this month 
  155.              calendar.setTime(curDate); 
  156.              calendar.add(Calendar.MONTH, 1); 
  157.              calendar.set(Calendar.DAY_OF_MONTH, 0); 
  158.              // Log.d(TAG, "m:" + calendar.get(Calendar.MONTH) + " d:" + 
  159.              // calendar.get(Calendar.DAY_OF_MONTH)); 
  160.              int monthDay = calendar.get(Calendar.DAY_OF_MONTH); 
  161.              for (int i = 1; i < monthDay; i++) { 
  162.                    date[monthStart + i] = i + 1; 
  163.              } 
  164.              curEndIndex = monthStart + monthDay; 
  165.              // next month 
  166.              for (int i = monthStart + monthDay; i < 42; i++) { 
  167.                    date[i] = i - (monthStart + monthDay) + 1; 
  168.              } 
  169.              if (curEndIndex < 42) { 
  170.                    // 显示了下一月的 
  171.                    calendar.add(Calendar.DAY_OF_MONTH, 1); 
  172.              } 
  173.              calendar.set(Calendar.DAY_OF_MONTH, date[41]); 
  174.              showLastDate = calendar.getTime(); 
  175.        } 
  176.       
  177.        /** 
  178.        * 
  179.        * @param canvas 
  180.        * @param index 
  181.        * @param text 
  182.        */ 
  183.        private void drawCellText(Canvas canvas, int index, String text, int color) { 
  184.              int x = getXByIndex(index); 
  185.              int y = getYByIndex(index); 
  186.              surface.datePaint.setColor(color); 
  187.              float cellY = surface.monthHeight + surface.weekHeight + (y - 1) 
  188.              * surface.cellHeight + surface.cellHeight * 3 / 4f; 
  189.              float cellX = (surface.cellWidth * (x - 1)) 
  190.              + (surface.cellWidth - surface.datePaint.measureText(text)) 
  191.              / 2f; 
  192.              canvas.drawText(text, cellX, cellY, surface.datePaint); 
  193.        } 
  194.       
  195.        /** 
  196.        * 
  197.        * @param canvas 
  198.        * @param index 
  199.        * @param color 
  200.        */ 
  201.        private void drawCellBg(Canvas canvas, int index, int color) { 
  202.              int x = getXByIndex(index); 
  203.              int y = getYByIndex(index); 
  204.              surface.cellBgPaint.setColor(color); 
  205.              float left = surface.cellWidth * (x - 1) + surface.borderWidth; 
  206.              float top = surface.monthHeight + surface.weekHeight + (y - 1) 
  207.              * surface.cellHeight + surface.borderWidth; 
  208.              canvas.drawRect(left, top, left + surface.cellWidth 
  209.              - surface.borderWidth, top + surface.cellHeight 
  210.              - surface.borderWidth, surface.cellBgPaint); 
  211.        } 
  212.       
  213.        private void drawDownOrSelectedBg(Canvas canvas) { 
  214.              // down and not up 
  215.              if (downDate != null) { 
  216.                    drawCellBg(canvas, downIndex, surface.cellDownColor); 
  217.              } 
  218.              // selected bg color 
  219.              if (!selectedEndDate.before(showFirstDate) 
  220.              && !selectedStartDate.after(showLastDate)) { 
  221.              int[] section = new int[] { -1, -1 }; 
  222.                    calendar.setTime(curDate); 
  223.                    calendar.add(Calendar.MONTH, -1); 
  224.                    findSelectedIndex(0, curStartIndex, calendar, section); 
  225.                    if (section[1] == -1) { 
  226.                          calendar.setTime(curDate); 
  227.                          findSelectedIndex(curStartIndex, curEndIndex, calendar, section); 
  228.                    } 
  229.                    if (section[1] == -1) { 
  230.                          calendar.setTime(curDate); 
  231.                          calendar.add(Calendar.MONTH, 1); 
  232.                          findSelectedIndex(curEndIndex, 42, calendar, section); 
  233.                    } 
  234.                    if (section[0] == -1) { 
  235.                          section[0] = 0; 
  236.                    } 
  237.                    if (section[1] == -1) { 
  238.                          section[1] = 41; 
  239.                    } 
  240.                    for (int i = section[0]; i <= section[1]; i++) { 
  241.                          drawCellBg(canvas, i, surface.cellSelectedColor); 
  242.                    } 
  243.              } 
  244.        } 
  245.       
  246.        private void findSelectedIndex(int startIndex, int endIndex, 
  247.        Calendar calendar, int[] section) { 
  248.              for (int i = startIndex; i < endIndex; i++) { 
  249.                    calendar.set(Calendar.DAY_OF_MONTH, date[i]); 
  250.                    Date temp = calendar.getTime(); 
  251.                    // Log.d(TAG, "temp:" + temp.toLocaleString()); 
  252.                    if (temp.compareTo(selectedStartDate) == 0) { 
  253.                          section[0] = i; 
  254.                    } 
  255.                    if (temp.compareTo(selectedEndDate) == 0) { 
  256.                          section[1] = i; 
  257.                          return; 
  258.                    } 
  259.              } 
  260.        } 
  261.       
  262.        public Date getSelectedStartDate() { 
  263.              return selectedStartDate; 
  264.        } 
  265.       
  266.        public Date getSelectedEndDate() { 
  267.              return selectedEndDate; 
  268.        } 
  269.       
  270.        private boolean isLastMonth(int i) { 
  271.              if (i < curStartIndex) { 
  272.                    return true; 
  273.              } 
  274.              return false; 
  275.        } 
  276.       
  277.        private boolean isNextMonth(int i) { 
  278.              if (i >= curEndIndex) { 
  279.                    return true; 
  280.              } 
  281.              return false; 
  282.        } 
  283.       
  284.        private int getXByIndex(int i) { 
  285.              return i % 7 + 1; // 1 2 3 4 5 6 7 
  286.        } 
  287.       
  288.        private int getYByIndex(int i) { 
  289.              return i / 7 + 1; // 1 2 3 4 5 6 
  290.        } 
  291.       
  292.        // 获得当前应该显示的年月 
  293.        public String getYearAndmonth() { 
  294.              calendar.setTime(curDate); 
  295.              int year = calendar.get(Calendar.YEAR); 
  296.              int month = calendar.get(Calendar.MONTH); 
  297.              return year + "-" + surface.monthText[month]; 
  298.        } 
  299.       
  300.        //上一月 
  301.        public String clickLeftMonth(){ 
  302.              calendar.setTime(curDate); 
  303.              calendar.add(Calendar.MONTH, -1); 
  304.              curDate = calendar.getTime(); 
  305.              invalidate(); 
  306.              return getYearAndmonth(); 
  307.        } 
  308.        //下一月 
  309.        public String clickRightMonth(){ 
  310.              calendar.setTime(curDate); 
  311.              calendar.add(Calendar.MONTH, 1); 
  312.              curDate = calendar.getTime(); 
  313.              invalidate(); 
  314.              return getYearAndmonth(); 
  315.        } 
  316.       
  317.        private void setSelectedDateByCoor(float x, float y) { 
  318.              // change month 
  319.             // if (y < surface.monthHeight) { 
  320.                   // // pre month 
  321.                   // if (x < surface.monthChangeWidth) { 
  322.                         // calendar.setTime(curDate); 
  323.                         // calendar.add(Calendar.MONTH, -1); 
  324.                         // curDate = calendar.getTime(); 
  325.                   // } 
  326.                   // // next month 
  327.                   // else if (x > surface.width - surface.monthChangeWidth) { 
  328.                         // calendar.setTime(curDate); 
  329.                         // calendar.add(Calendar.MONTH, 1); 
  330.                         // curDate = calendar.getTime(); 
  331.                   // } 
  332.             // } 
  333.              // cell click down 
  334.              if (y > surface.monthHeight + surface.weekHeight) { 
  335.                    int m = (int) (Math.floor(x / surface.cellWidth) + 1); 
  336.                    int n = (int) (Math 
  337.                    .floor((y - (surface.monthHeight + surface.weekHeight)) 
  338.                    / Float.valueOf(surface.cellHeight)) + 1); 
  339.                    downIndex = (n - 1) * 7 + m - 1; 
  340.                    Log.d(TAG, "downIndex:" + downIndex); 
  341.                    calendar.setTime(curDate); 
  342.                    if (isLastMonth(downIndex)) { 
  343.                          calendar.add(Calendar.MONTH, -1); 
  344.                    } else if (isNextMonth(downIndex)) { 
  345.                          calendar.add(Calendar.MONTH, 1); 
  346.                    } 
  347.                    calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]); 
  348.                    downDate = calendar.getTime(); 
  349.              } 
  350.              invalidate(); 
  351.        } 
  352.       
  353.        @Override 
  354.        public boolean onTouch(View v, MotionEvent event) { 
  355.              switch (event.getAction()) { 
  356.                    case MotionEvent.ACTION_DOWN: 
  357.                    setSelectedDateByCoor(event.getX(), event.getY()); 
  358.                    break; 
  359.                    case MotionEvent.ACTION_UP: 
  360.                    if (downDate != null) { 
  361.                         // if (!completed) { 
  362.                               // if (downDate.before(selectedStartDate)) { 
  363.                                     // selectedEndDate = selectedStartDate; 
  364.                                     // selectedStartDate = downDate; 
  365.                               // } else { 
  366.                               // selectedEndDate = downDate; 
  367.                         // } 
  368.                         // completed = true; 
  369.                   // } else { 
  370.                   // selectedStartDate = selectedEndDate = downDate; 
  371.                   // completed = false; 
  372.             // } 
  373.              selectedStartDate = selectedEndDate = downDate; 
  374.              //响应监听事件 
  375.              onItemClickListener.OnItemClick(selectedStartDate); 
  376.              // Log.d(TAG, "downdate:" + downDate.toLocaleString()); 
  377.              //Log.d(TAG, "start:" + selectedStartDate.toLocaleString()); 
  378.              //Log.d(TAG, "end:" + selectedEndDate.toLocaleString()); 
  379.              downDate = null; 
  380.              invalidate(); 
  381.        } 
  382.        break; 
  383.  } 
  384.  return true; 
  385.  } 
  386.  //给控件设置监听事件 
  387.  public void setOnItemClickListener(OnItemClickListener onItemClickListener){ 
  388.        this.onItemClickListener = onItemClickListener; 
  389.  } 
  390.  //监听接口 
  391.  public interface OnItemClickListener { 
  392.        void OnItemClick(Date date); 
  393.  } 
  394.  /** 
  395.  * 
  396.  * 1. 布局尺寸 2. 文字颜色,大小 3. 当前日期的颜色,选择的日期颜色 
  397.  */ 
  398.  private class Surface { 
  399.        public float density; 
  400.        public int width; // 整个控件的宽度 
  401.        public int height; // 整个控件的高度 
  402.        public float monthHeight; // 显示月的高度 
  403.        //public float monthChangeWidth; // 上一月、下一月按钮宽度 
  404.        public float weekHeight; // 显示星期的高度 
  405.        public float cellWidth; // 日期方框宽度 
  406.        public float cellHeight; // 日期方框高度 
  407.        public float borderWidth; 
  408.        public int bgColor = Color.parseColor("#FFFFFF"); 
  409.        private int textColor = Color.BLACK; 
  410.        //private int textColorUnimportant = Color.parseColor("#666666"); 
  411.        private int btnColor = Color.parseColor("#666666"); 
  412.        private int borderColor = Color.parseColor("#CCCCCC"); 
  413.        public int todayNumberColor = Color.RED; 
  414.        public int cellDownColor = Color.parseColor("#CCFFFF"); 
  415.        public int cellSelectedColor = Color.parseColor("#99CCFF"); 
  416.        public Paint borderPaint; 
  417.        public Paint monthPaint; 
  418.        public Paint weekPaint; 
  419.        public Paint datePaint; 
  420.        public Paint monthChangeBtnPaint; 
  421.        public Paint cellBgPaint; 
  422.        public Path boxPath; // 边框路径 
  423.        //public Path preMonthBtnPath; // 上一月按钮三角形 
  424.        //public Path nextMonthBtnPath; // 下一月按钮三角形 
  425.  public String[] weekText = { "Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 
  426.  public String[] monthText = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
  427.       
  428.        public void init() { 
  429.              float temp = height / 7f; 
  430.              monthHeight = 0;//(float) ((temp + temp * 0.3f) * 0.6); 
  431.              //monthChangeWidth = monthHeight * 1.5f; 
  432.              weekHeight = (float) ((temp + temp * 0.3f) * 0.7); 
  433.              cellHeight = (height - monthHeight - weekHeight) / 6f; 
  434.              cellWidth = width / 7f; 
  435.              borderPaint = new Paint(); 
  436.              borderPaint.setColor(borderColor); 
  437.              borderPaint.setStyle(Paint.Style.STROKE); 
  438.              borderWidth = (float) (0.5 * density); 
  439.              // Log.d(TAG, "borderwidth:" + borderWidth); 
  440.              borderWidth = borderWidth < 1 ? 1 : borderWidth; 
  441.              borderPaint.setStrokeWidth(borderWidth); 
  442.              monthPaint = new Paint(); 
  443.              monthPaint.setColor(textColor); 
  444.              monthPaint.setAntiAlias(true); 
  445.              float textSize = cellHeight * 0.4f; 
  446.              Log.d(TAG, "text size:" + textSize); 
  447.              monthPaint.setTextSize(textSize); 
  448.              monthPaint.setTypeface(Typeface.DEFAULT_BOLD); 
  449.              weekPaint = new Paint(); 
  450.              weekPaint.setColor(textColor); 
  451.              weekPaint.setAntiAlias(true); 
  452.              float weekTextSize = weekHeight * 0.6f; 
  453.              weekPaint.setTextSize(weekTextSize); 
  454.              weekPaint.setTypeface(Typeface.DEFAULT_BOLD); 
  455.              datePaint = new Paint(); 
  456.              datePaint.setColor(textColor); 
  457.              datePaint.setAntiAlias(true); 
  458.              float cellTextSize = cellHeight * 0.5f; 
  459.              datePaint.setTextSize(cellTextSize); 
  460.              datePaint.setTypeface(Typeface.DEFAULT_BOLD); 
  461.              boxPath = new Path(); 
  462.              //boxPath.addRect(0, 0, width, height, Direction.CW); 
  463.              //boxPath.moveTo(0, monthHeight); 
  464.              boxPath.rLineTo(width, 0); 
  465.              boxPath.moveTo(0, monthHeight + weekHeight); 
  466.              boxPath.rLineTo(width, 0); 
  467.              for (int i = 1; i < 6; i++) { 
  468.                    boxPath.moveTo(0, monthHeight + weekHeight + i * cellHeight); 
  469.                    boxPath.rLineTo(width, 0); 
  470.                    boxPath.moveTo(i * cellWidth, monthHeight); 
  471.                    boxPath.rLineTo(0, height - monthHeight); 
  472.              } 
  473.              boxPath.moveTo(6 * cellWidth, monthHeight); 
  474.              boxPath.rLineTo(0, height - monthHeight); 
  475.              //preMonthBtnPath = new Path(); 
  476.              //int btnHeight = (int) (monthHeight * 0.6f); 
  477.              //preMonthBtnPath.moveTo(monthChangeWidth / 2f, monthHeight / 2f); 
  478.              //preMonthBtnPath.rLineTo(btnHeight / 2f, -btnHeight / 2f); 
  479.              //preMonthBtnPath.rLineTo(0, btnHeight); 
  480.              //preMonthBtnPath.close(); 
  481.              //nextMonthBtnPath = new Path(); 
  482.              //nextMonthBtnPath.moveTo(width - monthChangeWidth / 2f, 
  483.              // monthHeight / 2f); 
  484.              //nextMonthBtnPath.rLineTo(-btnHeight / 2f, -btnHeight / 2f); 
  485.              //nextMonthBtnPath.rLineTo(0, btnHeight); 
  486.              //nextMonthBtnPath.close(); 
  487.              monthChangeBtnPaint = new Paint(); 
  488.              monthChangeBtnPaint.setAntiAlias(true); 
  489.              monthChangeBtnPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
  490.              monthChangeBtnPaint.setColor(btnColor); 
  491.              cellBgPaint = new Paint(); 
  492.              cellBgPaint.setAntiAlias(true); 
  493.              cellBgPaint.setStyle(Paint.Style.FILL); 
  494.              cellBgPaint.setColor(cellSelectedColor); 
  495.        } 
  496.  } 


实现日历控件:

[html]

  1. <RelativeLayout  
  2.     android:id="@+id/layout_calendar"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:visibility="visible" >  
  6.     <TextView  
  7.         android:id="@+id/calendarCenter"  
  8.         style="@style/main_bar_text_style"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_margin="8dp" />  
  13.     <ImageButton  
  14.         android:id="@+id/calendarLeft"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_alignParentLeft="true"  
  18.         android:padding="8dp"  
  19.         android:contentDescription="@null"  
  20.         android:src="@drawable/calendar_month_left"   
  21.         android:background="@null"/>  
  22.     <ImageButton  
  23.         android:id="@+id/calendarRight"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_alignParentRight="true"  
  27.         android:padding="8dp"  
  28.         android:contentDescription="@null"  
  29.         android:src="@drawable/calendar_month_right"   
  30.         android:background="@null"/>  
  31.     <com.techrare.view.CalendarView  
  32.         android:id="@+id/calendar"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_alignParentLeft="true"  
  36.         android:layout_below="@+id/calendarCenter" />  
  37. </RelativeLayout>  


[html]

  1. <style name="main_bar_text_style">  
  2.     <item name="android:textColor">@color/white</item>  
  3.     <item name="android:textStyle">bold</item>  
  4.     <item name="android:textSize">18sp</item>  
  5. </style>  



上一月图片:

北京联动北方科技有限公司

[java]


调用 calendar.clickLeftMonth();   

调用 calendar.clickLeftMonth();

     

下一月:

北京联动北方科技有限公司

[java]


调用 calendar.clickRightMonth();   

调用 calendar.clickRightMonth();



日历控件中的一些功能(可以自己加):

[java]
//获取日历控件对象   

  1. calendar = (CalendarView)findViewById(R.id.calendar); 
  2. //获取日历中年月 ya[0]为年,ya[1]为月(格式大家可以自行在日历控件中改) 
  3. String[] ya = calendar.getYearAndmonth().split("-"); 
  4. //点击上一月 同样返回年月 
  5. String leftYearAndmonth = calendar.clickLeftMonth(); 
  6. String[] lya = leftYearAndmonth.split("-"); 
  7. //点击下一月 
  8. String rightYearAndmonth = calendar.clickRightMonth(); 
  9. String[] rya = rightYearAndmonth.split("-"); 
  10. //设置控件监听,可以监听到点击的每一天(大家也可以在控件中自行设定) 
  11. calendar.setOnItemClickListener(new calendarItemClickListener()); 
  12. class calendarItemClickListener implements OnItemClickListener{ 
  13.        @Override 
  14.        public void OnItemClick(Date date) { 
  15.              Toast.makeText(getApplicationContext(), date+"", Toast.LENGTH_SHORT).show(); 
  16.        } 
  17.  } 


//获取日历控件对象
calendar = (CalendarView)findViewById(R.id.calendar);
//获取日历中年月 ya[0]为年,ya[1]为月(格式大家可以自行在日历控件中改)
String[] ya = calendar.getYearAndmonth().split("-");
//点击上一月 同样返回年月
String leftYearAndmonth = calendar.clickLeftMonth();
String[] lya = leftYearAndmonth.split("-");
//点击下一月
String rightYearAndmonth = calendar.clickRightMonth();
String[] rya = rightYearAndmonth.split("-");
//设置控件监听,可以监听到点击的每一天(大家也可以在控件中自行设定)
calendar.setOnItemClickListener(new calendarItemClickListener());
class calendarItemClickListener implements OnItemClickListener{
       @Override
       public void OnItemClick(Date date) {
             Toast.makeText(getApplicationContext(), date+"", Toast.LENGTH_SHORT).show();
       }
}



这些功能只是配合项目中的需要来添加的,大家如有其他需求,可以自行在控件中添加,应该也不怎么困难.




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论