android:最简单的方式实现自定义选择时间分钟间隔,非滚轮方式

目前项目只需要分钟的间隔,所以小时和月的间隔没有做,spiner有坑,做的不好,以后有空再重写一下;

解释参数

private int MAX_LIMITE_DAY = 30;//最大可选日期
private int EVERY_MININTES_SPCE= 10;//分钟间隙
private int MIN_MININTES_AFTER = 15;//当前时间到最近可用时间的间隔(最小时间段);

最小时间间隔的解释,

如果分钟间隙为1,最小间隔是1,那么就是自然分钟数1-59;


最小间隔15分钟 如果当前时间是10:00 那么最近一次可用的时间为 10:20,因为分钟每10分钟一个档,15分钟在第二档;

最小间隔15分钟 如果当前时间是10:40 那么最近一次可用的时间为 11:00;


最小间隔10分钟 如果当前世界是10:00,那么最近一次可用的时间为 11:10;10分钟一个档;

代码

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;

public class TimePickerActivity extends BaseActivity {

	private int MAX_LIMITE_DAY = 30;//最大可选日期
	private int EVERY_MININTES_SPCE= 10;//分钟间隙
	private int MIN_MININTES_AFTER = 15;//最小时间段
	
	
	private List<String> listDates = new ArrayList<String>(),
			listHours = new ArrayList<String>(),
			listMinuts = new ArrayList<String>();

	private 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;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_picker_time);
		initDefultBookTime();
		initUI();
		initData();
	}

	@Override
	protected 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);

		datesAdapter = 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("可预约"+MIN_MININTES_AFTER+"分钟后,"+MAX_LIMITE_DAY+"天内的车辆");
		
		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;
		}
	}
	
	@Override
	protected 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 < 24; i++) {
				listHours.add(i + "点");
			}
			tvChooseTip.setText("请选择一个时间点");
			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));
				day.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 < 24; i++) {
				listHours.add(i + "点");
			}
			calendar.set(Calendar.HOUR_OF_DAY, currentHour+1);
		}else{
			for (int i = currentHour; i < 24; i++) {
				listHours.add(i + "点");
			}
			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 ="今天";
			break;
		case 1:
			tweek ="明天";
			break;
		case 2:
			tweek ="后天";
			break;
		default: {
			switch (week) {
			case 2:
				tweek ="周一";
				break;
			case 3:
				tweek ="周二";
				break;
			case 4:
				tweek ="周三";
				break;
			case 5:
				tweek ="周四";
				break;
			case 6:
				tweek ="周五";
				break;
			case 7:
				tweek ="周六";
				break;
			case 1:
				tweek ="周日";
				break;
			}
		}
			break;
		}
		StringBuilder text = new StringBuilder();
		text.append(month + 1).append("月");
		text.append(day).append("日");
		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){//第一版版本问题,先这样吧;
			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<=59;index+=EVERY_MININTES_SPCE){
			if(index<0) continue;
			listMinuts.add(buildTimeTextExt(index)+"分");
		}
	}

	private String buildTimeText() {
		StringBuilder text = new StringBuilder();
		text.append(calendar.get(Calendar.MONTH) + 1).append("月");
		text.append(buildTimeTextExt(calendar.get(Calendar.DAY_OF_MONTH))).append(
				"日");
		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 < 10)
			return "0" + value;
		else
			return value + "";
	}

	private View.OnClickListener viewListener = new View.OnClickListener() {
		@Override
		public 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);
		//选择今天
		if (currentDay==checkDay&¤tHour == checkHour) {
			initDates();
		}else if(currentDay==checkDay){
			//天相同,但是初始化时被加过一天,重新计算小时和分钟数
			if(isAddNewHour&¤tHour==checkHour-1){
				initHours();
			}else{
				//如果被加过一天,跳过已经加过的那天
				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 < 24; i++) {
			listHours.add(i + "点");
		}
		buildMinutes(minute, false);
		
		hourAdapter.notifyDataSetChanged();
		minuteAdapter.notifyDataSetChanged();
		
		if(!isOnlyHour){
			int hours = checkHour;
			int mnites =currentMinute;
			try{
				hours = Integer.parseInt(spHour.getSelectedItem().toString().replace("点", "").trim());
				mnites = Integer.parseInt(spMinute.getSelectedItem().toString().replace("分", "").trim());
			}catch(Exception ex){
			}
			calendar.set(Calendar.HOUR_OF_DAY, hours);
			calendar.set(Calendar.MINUTE, mnites);
		}
	}

	private OnItemSelectedListener itemSelectListener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			switch (parent.getId()) {
			case R.id.sp_date:
				int selectDay=currentDay + 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("点", "").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("分", "").trim());
				calendar.set(Calendar.MINUTE, mnite);
				break;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {

		}
	};


	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
	}
}


效果图

技术分享

技术分享


技术分享


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。