In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Android how to customize the implementation of calendar controls, the article introduces in great detail, has a certain reference value, interested friends must read it!
The details are as follows
1. Calendar class
two。 Overall Arrangement
Create calendar_layout.xml
Create item_layout.xml
In activity_main.xml
3. Business processing
Create a NewCalendar class that inherits LinearLayout
Public class NewCalendar extends LinearLayout {private TextView lastTv,nextTv,dateTv; private GridView calendarGv; private Calendar calendar=Calendar.getInstance (); / / Calendar control initializes / / rewrites three constructors public NewCalendar (Context context) {super (context);} public NewCalendar (Context context, @ Nullable AttributeSet attrs) {super (context, attrs); initControl (context) / / bound control} public NewCalendar (Context context, @ Nullable AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); initControl (context); / / bound control} private void initControl (Context context) {bindControl (context); / / bound control bindControlEvent () / / bind control event} / / bind control event method private void bindControlEvent () {renderCalendar (); / / "next month" click event nextTv.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {calendar.add (Calendar.MONTH,+1); / / month + 1 renderCalendar () }}); / / previous click event lastTv.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {calendar.add (Calendar.MONTH,-1); / / month-1 renderCalendar ();}}) } private void renderCalendar () {SimpleDateFormat sdf = new SimpleDateFormat ("MMM yyy"); / / date format dateTv.setText (sdf.format (calendar.getTime (); / / set month ArrayList cells = new ArrayList (); Calendar calendar1 = (Calendar) calendar.clone (); / / Clone calendar object calendar1.set (Calendar.DAY_OF_MONTH, 1); / / place on the first day of the month Int prevDays = calendar1.get (Calendar.DAY_OF_WEEK)-1; / / get what day the last day of the last month was calendar1.add (Calendar.DAY_OF_MONTH,-prevDays); / / the first day int maxCount = 6 * 7 / / set the maximum number of days per month / / cycle into the collection while (cells.size () < maxCount) {cells.add (calendar1.getTime ()); calendar1.add (Calendar.DAY_OF_MONTH, 1); / / date + 1} / / set adapter calendarGv.setAdapter (new CalendarAdapter (getContext (), cells)) } / / Adapter private class CalendarAdapter extends ArrayAdapter {LayoutInflater layoutInflater; public CalendarAdapter (@ NonNull Context context,ArrayList days) {super (context, R.layout.itemhammer layoutdays); layoutInflater=LayoutInflater.from (context) } @ NonNull @ Override public View getView (int position, @ Nullable View convertView, @ NonNull ViewGroup parent) {Date date=getItem (position); ViewHolder viewHolder; if (convertView==null) {/ / initialize binding convertView=layoutInflater.inflate (R.layout.itemhammer layout.parentMagne false); viewHolder=new ViewHolder () ViewHolder.itemTv=convertView.findViewById (R.id.itemTv); convertView.setTag (viewHolder);} viewHolder= (ViewHolder) convertView.getTag (); int day=date.getDate (); viewHolder.itemTv.setText (String.valueOf (day)); / / assign return convertView;} class ViewHolder {TextView itemTv }} private void bindControl (Context context) {LayoutInflater inflater=LayoutInflater.from (context); inflater.inflate (R.layout.roomaricing layoutthis); lastTv=findViewById (R.id.lastTv); nextTv=findViewById (R.id.nextTv); dateTv=findViewById (R.id.dateTv); calendarGv=findViewById (R.id.calendarGv);}}
3. Custom UI
In the adapter getView () method, personalize the calendar interface
Date now=new Date (); Boolean isTheSameMonth=false; / / whether the date displayed is the same as the current month / / determine whether the date and month displayed is the same as the current month if (date.getMonth () = = now.getMonth ()) {/ / the same isTheSameMonth= true } / / if the date month displayed is the same as the current month, set the font color to black if (isTheSameMonth) {viewHolder.itemTv.setTextColor (Color.parseColor ("# 000000")) } / / set the current date font to red if (now.getDate () = = date.getDate () & & now.getMonth () = = date.getMonth () & & now.getYear () = = date.getYear ()) {viewHolder.itemTv.setTextColor (Color.parseColor ("# ff0000"));}
4. Event monitoring
In NewCalendar, first write the interface for the long press event, and then set the adapter click event
/ / long press event interface public interface NewCalendarListener {void onItemClick (Date date);} / / Adapter long press event calendarGv.setOnItemLongClickListener (new AdapterView.OnItemLongClickListener () {@ Override public boolean onItemLongClick (AdapterView parent, View view, int position, long id) {if (listener==null) {return false) } else {/ / get the location of the long press, and pass listener.onItemClick ((Date) parent.getItemAtPosition (position)) in the onItemClick method; return true;})
In MainActivity, the implementation of long press event interface and override method, achieve long press a date pop-up Toast to show the current long press date.
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); NewCalendar calendar=findViewById (R.id.calendarNc); calendar.listener=this;} / / MainActivity implements the long press event interface @ Override public void onItemClick (Date date) {DateFormat df= SimpleDateFormat.getDateInstance (); Toast.makeText (this,df.format (date), Toast.LENGTH_SHORT). Show ();}
These are all the contents of the article "how to customize the Calendar Control by Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.