Android: The simplest way to implement a custom selection time-of-minute interval, non-roller mode

Source: Internet
Author: User
Tags spdate

The current project only needs a minute interval, so the interval between hours and months did not do, Spiner have pits, do not do well, later have time to rewrite;

Interpreting parameters

private int max_limite_day = 30;//maximum selectable date
private int every_minintes_spce= 10;//minute gap
private int min_minintes_after = 15;//The current time to the last available time interval (minimum time period);

Explanation of the minimum time interval,

If the minute gap is 1, the minimum interval is 1, then the natural minute number is 1-59;


Minimum interval 15 minutes if the current time is 10:00 then the last available time is 10:20, because the minute every 10 minutes a file, 15 minutes in the second gear;

Minimum interval 15 minutes if the current time is 10:40 then the last available time is 11:00;


Minimum interval 10 minutes if the current world is 10:00, then the last available time is 11:10; 10 minutes a file;

Code

Package Com.idonoo.sharecar.ui.commom.activitys;import Java.util.arraylist;import Java.util.calendar;import Java.util.list;import Java.util.timezone;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.spinner;import Android.widget.textview;import Com.idonoo.frame.model.booktimerule;import Com.idonoo.sharecar.r;import Com.idonoo.sharecar.app.intentextra;import com.idonoo.shareCar.uiframe.BaseActivity ;p ublic class Timepickeractivity extends baseactivity {private int max_limite_day = 30;//Maximum optional date private int every_minintes _spce= 10;//minute gap private int min_minintes_after = 15;//minimum time period private list<string> listdates = new arraylist< String> (), listhours = new arraylist<string> (), listminuts = new arraylist<string> ();p rivate Spinner Spdate, Sphour, Spminute;private arrayadapteR<string> Datesadapter, Houradapter, minuteadapter;private TextView tvchoosetip;private calendar calendar; private int currentyear;private int currentmonth;private int currentday;private int currenthour;private int Currentminute;private Boolean isaddnewhour = False,isonlyhour=false; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_picker_time); Initdefultbooktime (); Initui (); InitData ();} @Overrideprotected void Initui () {Super.initui (); TextView Tvcacel = (TextView) Findviewbyid (r.id.cacel); TextView Tvselect = (TextView) Findviewbyid (r.id.select); Tvcacel.setonclicklistener (Viewlistener); Tvselect.setonclicklistener (Viewlistener); Findviewbyid (R.id.linear_empty). Setonclicklistener (ViewListener); Spdate = (Spinner) Findviewbyid (r.id.sp_date); sphour = (Spinner) Findviewbyid (r.id.sp_hour); Spminute = (Spinner) Findviewbyid (r.id.sp_minut);d atesadapter = new arrayadapter<string> (Getapplicationcontext (), R.layout. Layout_spinner_item_text, R.id.tv_spinner_text, listdates); Spdate.setadapter (datesadapter); hourAdapter = new Arrayadapter<string> (Getapplicationcontext (), R.layout.layout_spinner_item_text, R.id.tv_spinner_text, listhours); Sphour.setadapter (houradapter); minuteadapter = new Arrayadapter<string> (GetApplicationContext (), R.layout.layout_spinner_item_text, R.id.tv_spinner_text, listminuts); Spminute.setadapter (MinuteAdapter); Tvchoosetip = ((TextView) Findviewbyid (R.id.tv_choose_tip)) Tvchoosetip.settext ("can be reserved" +min_minintes_after+ "minutes later" + max_limite_day+ "vehicles within days"); Setviewclicklistener (Viewlistener, R.id.tv_add_day,r.id.tv_add_hour,r.id.tv_add_minute, R.id.tv_sub_day,r.id.tv_sub_hour,r.id.tv_sub_minute);} private void Initdefultbooktime () {int max = Booktimerule.getbookmaxtime (); if (Max > 0) {max_limite_day = max;} int min = Booktimerule.getbookmintime (); if (min > 0) {min_minintes_after = min;} int interval = Booktimerule.getbookintervaltime (); if (Interval > 0) {every_minintes_spce =Interval;}} @Overrideprotected void InitData () {calendar = Calendar.getinstance (Timezone.gettimezone ("gmt+08:00")); currentyear = Calendar.get (calendar.year); currentmonth = Calendar.get (calendar.month); currentday = Calendar.get (Calendar.DAY_OF_ MONTH); currenthour = Calendar.get (calendar.hour_of_day); currentminute = Calendar.get (Calendar.minute); Spdate.setonitemselectedlistener (Itemselectlistener); Sphour.setonitemselectedlistener (ItemSelectListener); Spminute.setonitemselectedlistener (Itemselectlistener); Isonlyhour=getintent (). Getbooleanextra ("OnlyHour", False if (isonlyhour) {Calendar.add (Calendar.day_of_month, 1), for (int i = 0; i <; i++) {Listhours.add (i + "dot");} Tvchoosetip.settext ("Please select a point in Time"), Findviewbyid (R.id.tv_add_day) setvisibility (View.gone); Findviewbyid (R.id.tv_ Add_minute). setvisibility (View.gone); Findviewbyid (R.id.tv_sub_hour). setvisibility (View.gone); FindViewById ( R.id.tv_sub_minute). setvisibility (View.gone); spdate.setvisibility (View.gone); Spminute.setvisibility (View.GONE) ; hOuradapter.notifydatasetchanged ();} Else{initdates ();}} private void Initdates () {isaddnewhour = False;inithours (); Calendar day = calendar.getinstance (Timezone.gettimezone ("gmt+08:00")), Listdates.clear (); for (int i=0; i < Max_ Limite_day; i++) {if (isaddnewhour&¤thour==23) {Day.add (calendar.day_of_month, 1); Listdates.add (Buildday (Day));} Else{listdates.add (Buildday (day));d Ay.add (Calendar.day_of_month, 1);}} Datesadapter.notifydatasetchanged ();} private void Inithours () {isaddnewhour=false;listminuts.clear (); Listhours.clear (); Buildminutes (currentminute,true if (Isaddnewhour) {for (int i = currenthour+1; i <; i++) {Listhours.add (i + "dot");} Calendar.set (Calendar.hour_of_day, currenthour+1);} else{for (int i = currenthour; i <; i++) {Listhours.add (i + "point");} Calendar.set (Calendar.hour_of_day, currenthour);} Houradapter.notifydatasetchanged (); minuteadapter.notifydatasetchanged ();} Private String buildday (Calendar calendar) {int day = Calendar.get (calendar.day_of_month); int MONTH = CaleNdar.get (calendar.month); int week = Calendar.get (Calendar.day_of_week); Calendar Tcalendar = calendar.getinstance (Timezone.gettimezone ("gmt+08:00")); int today = Tcalendar.get (Calendar.day_ of_month); int tmonth= tcalendar.get (calendar.month); int space = Day-today;if (tmonth!=month) space =-1; String Tweek = "", switch (space) {Case 0:tweek = "Today"; Break;case 1:tweek = "Tomorrow"; break;case 2:tweek = "acquired"; Break;default: {SWI TCH (week) {case 2:tweek = "Monday"; break;case 3:tweek = "Tuesday"; break;case 4:tweek = "Wednesday"; break;case 5:tweek = "Thursday"; Break;case 6:tw Eek = "Friday"; break;case 7:tweek = "Saturday"; break;case 1:tweek = "Sunday"; break;}} break;} StringBuilder Text = new StringBuilder (); Text.append (month + 1). Append ("Moon"); Text.append (day) append ("th"); text.append ("("); Text.append (Tweek); Text.append (")"); return text.tostring ();} private void buildminutes (int giveminutes,boolean isassociatehour) {int destminute = Giveminutes+min_minintes_after;if (giveminutes==-1) Destminute = -1;if (isassociatehour) {//The first version of the issue, let's do it; if (destminute>=60){Destminute = Destminute-60;isaddnewhour = true;} Else{isaddnewhour = false;}} Listminuts.clear (); int skip = (int) Math.ceil (double.valueof (Destminute)/double.valueof (EVERY_MININTES_SPCE)); int StartIndex = Every_minintes_spce*skip;if (startindex==60) {StartIndex = 0;isaddnewhour = true;} for (int index = STARTINDEX;INDEX&LT;=59;INDEX+=EVERY_MININTES_SPCE) {if (index<0) Continue;listminuts.add ( Buildtimetextext (Index) + "Min");}} Private String Buildtimetext () {StringBuilder text = new StringBuilder (); Text.append (Calendar.get (calendar.month) + 1). Append ("month"); Text.append (Buildtimetextext (Calendar.get (Calendar.day_of_month))). Append ("Day"); Text.append ( Buildtimetextext (Calendar.get (Calendar.hour_of_day))). Append (":"); Text.append (Buildtimetextext (Calendar.get ( Calendar.minute)); return text.tostring ();} Private String buildtimetextext (int value) {if (value <) return "0" + Value;elsereturn value + "";} Private View.onclicklistener Viewlistener = new View.onclicklistener () {@Overridepublic void OnClick(View v) {switch (V.getid ()) {case r.id.cacel:finish (); Break;case r.id.linear_empty:case R.id.select:calendar.set ( calendar.second,0); Intent Intent = new Intent (); Intent.putextra (Intentextra.extra_time_text, Buildtimetext ()); Intent.putextra (intentextra.extra_depart_time, calendar); Setresult (ACTIVITY.RESULT_OK, intent); finish (); break; Case R.id.tv_add_day:break;case r.id.tv_add_hour:break;case r.id.tv_add_minute:break;case R.id.tv_sub_day:break; Case R.id.tv_sub_hour:break;case R.id.tv_sub_minute:break;}}; private void Rebuilddates (Calendar calendar) {listhours.clear (); listminuts.clear (); int Checkday=calendar.get ( calendar.day_of_month); int checkhour=calendar.get (calendar.hour_of_day); int Checkminute=calendar.get ( Calendar.minute);//Select Today if (Currentday==checkday&¤thour = = Checkhour) {initdates ();} else if (currentday==checkday) {//day is the same, but is added one day at initialization, recalculate hours and minutes if (isaddnewhour&¤thour==checkhour-1) {inithours ();} else{//If a day is added, skip the day that has been added int hour=isaddnewhour?currenthour+1:currenthour;rebuilddatES (Hour,-1, Checkhour, Checkminute);}} Else{rebuilddates (0,-1, Checkhour, Checkminute);}} private void rebuilddates (int hour,int minute,int checkhour,int checkminute) {for (int i = hour; i <; i++) {listhours . Add (i + "dot");} Buildminutes (minute, false); houradapter.notifydatasetchanged (); minuteadapter.notifydatasetchanged (); if (! Isonlyhour) {int hours = Checkhour;int mnites =currentminute;try{hours = Integer.parseint (Sphour.getselecteditem (). ToString (). Replace ("point", ""). Trim ()); mnites = Integer.parseint (Spminute.getselecteditem (). toString (). Replace ("points", " "). Trim ());} catch (Exception ex) {}calendar.set (calendar.hour_of_day, hours); Calendar.set (Calendar.minute, mnites);}} Private Onitemselectedlistener Itemselectlistener = new Onitemselectedlistener () {@Overridepublic void onitemselected ( Adapterview<?> parent, View view,int position, long ID) {switch (Parent.getid ()) {case R.id.sp_date:int selectday=c Urrentday + position;if (position!=0&&isaddnewhour&¤thour==23) Selectday=seleCtday+1;calendar.set (Calendar.year, currentyear); Calendar.set (Calendar.month, Currentmonth); Calendar.set ( Calendar.day_of_month,selectday); rebuilddates (calendar); Break;case R.id.sp_hour:int hour = Integer.parseint ( Parent.getitematposition (position). toString (). Replace ("point", ""). Trim ()); Calendar.set (Calendar.hour_of_day, HOUR); Rebuilddates (calendar); Break;case r.id.sp_minut:int mnite = Integer.parseint (parent.getitematposition (position). ToString (). Replace ("Min", ""). Trim ()); Calendar.set (Calendar.minute, mnite); break;}} @Overridepublic void onnothingselected (adapterview<?> parent) {}}; @Overridepublic void Finish () {super.finish () ; Overridependingtransition (R.anim.slide_in_top, R.anim.slide_out_bottom);}}







Android: The simplest way to implement a custom selection time-of-minute interval, non-roller mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.