Android 仿iOS时间选择器自定义WheelView

ios的自带时间选择器很好看,但是android的自带时间选择器就各不相同,有的看起来还可以,但是有的就其丑无比,网上仿ios的自定义时间选择器 基本都是WheelView,上效果图吧,这是我修改过的结果。

Android 仿iOS时间选择器自定义WheelView_第1张图片          Android 仿iOS时间选择器自定义WheelView_第2张图片


/**
	 * 显示全部日期
	 */
	private void showDateAndTime(){

		Calendar c = Calendar.getInstance();
		int curYear = c.get(Calendar.YEAR);
		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
		int curDate = c.get(Calendar.DATE);

		final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
				.create();
		dialog.show();
		Window window = dialog.getWindow();
		// 设置布局
		window.setContentView(R.layout.my_new_pop_layout);
		// 设置宽高
		window.setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		// 设置弹出的动画效果
		window.setWindowAnimations(R.style.AnimBottom);

		month = (WheelView) window.findViewById(R.id.new_month);
		initMonth();
		day = (WheelView) window.findViewById(R.id.new_day);
		initDay(curYear,curMonth);
		hour = (WheelView) window.findViewById(R.id.new_hour);
		initHour();
		mins = (WheelView) window.findViewById(R.id.new_mins);
		initMins();

        // 设置当前时间
		//year.setCurrentItem(curYear - 1);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);
		hour.setCurrentItem(8);
		mins.setCurrentItem(30);

		month.setVisibleItems(7);
		day.setVisibleItems(7);
		hour.setVisibleItems(7);
		mins.setVisibleItems(7);

		// 设置监听
		TextView ok = (TextView) window.findViewById(R.id.set);
		TextView cancel = (TextView) window.findViewById(R.id.cancel);
		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String date = "2016" + "-"+ (month.getCurrentItem()+1)+"-"+(day.getCurrentItem()+1);
				String time = " " + hour.getCurrentItem()+ ":"+ (mins.getCurrentItem() + 1);
				Toast.makeText(MainActivity.this, date + time, Toast.LENGTH_LONG).show();
				dialog.cancel();
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
		cancelLayout.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				dialog.cancel();
				return false;
			}
		});
	}


	/**
	 * 显示时间
	 */
	private void showTimeDialog(){
		final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
				.create();
		dialog.show();
		Window window = dialog.getWindow();
		// 设置布局
		window.setContentView(R.layout.timepick);
		// 设置宽高
		window.setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		// 设置弹出的动画效果
		window.setWindowAnimations(R.style.AnimBottom);

		hour = (WheelView) window.findViewById(R.id.hour);
		initHour();
		mins = (WheelView) window.findViewById(R.id.mins);
		initMins();
		// 设置当前时间
		hour.setCurrentItem(8);
		mins.setCurrentItem(30);
		hour.setVisibleItems(7);
		mins.setVisibleItems(7);

		// 设置监听
		Button ok = (Button) window.findViewById(R.id.set);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str = hour.getCurrentItem() + ":"+ mins.getCurrentItem();
				Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
				dialog.cancel();
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
		cancelLayout.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				dialog.cancel();
				return false;
			}
		});
	}


	/**
	 * 显示日期
	 */
	private void showDateDialog() {
		final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
				.create();
		dialog.show();
		Window window = dialog.getWindow();
		// 设置布局
		window.setContentView(R.layout.datapick);
		// 设置宽高
		window.setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		// 设置弹出的动画效果
		window.setWindowAnimations(R.style.AnimBottom);


		Calendar c = Calendar.getInstance();
		int curYear = c.get(Calendar.YEAR);
		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
		int curDate = c.get(Calendar.DATE);
		year = (WheelView) window.findViewById(R.id.year);
		initYear(curYear);
		month = (WheelView) window.findViewById(R.id.month);
		initMonth();
		day = (WheelView) window.findViewById(R.id.day);
		initDay(curYear,curMonth);


		year.setCurrentItem(curYear - 1950);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);
		year.setVisibleItems(7);
		month.setVisibleItems(7);
		day.setVisibleItems(7);

		// 设置监听
		Button ok = (Button) window.findViewById(R.id.set);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str = (year.getCurrentItem()+1950) + "-"+ (month.getCurrentItem()+1)+"-"+(day.getCurrentItem()+1);
				Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
				dialog.cancel();
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
		cancelLayout.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				dialog.cancel();
				return false;
			}
		});
		// 因为我们用的是windows的方法,所以不管ok活cancel都要加上“dialog.cancel()”这句话,
		// 不然有程序崩溃的可能,仅仅是一种可能,但我们还是要排除这一点,对吧?
		// 用AlertDialog的两个Button,即使监听里什么也不写,点击后也是会吧dialog关掉的,不信的同学可以去试下

	}

/**
	 * 初始化年
	 */
	private void initYear(int curYear) {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1950, curYear);
		numericWheelAdapter.setLabel(" 年");
		//		numericWheelAdapter.setTextSize(15);  设置字体大小
		year.setViewAdapter(numericWheelAdapter);
		year.setCyclic(true);
	}

	/**
	 * 初始化月
	 */
	private void initMonth() {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 12, "%02d");
		numericWheelAdapter.setLabel(" 月");
		//		numericWheelAdapter.setTextSize(15);  设置字体大小
		month.setViewAdapter(numericWheelAdapter);
		month.setCyclic(true);
	}

	/**
	 * 初始化天
	 */
	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel(" 日");
		//		numericWheelAdapter.setTextSize(15);  设置字体大小
		day.setViewAdapter(numericWheelAdapter);
		day.setCyclic(true);
	}

	/**
	 * 初始化时
	 */
	private void initHour() {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 23, "%02d");
		numericWheelAdapter.setLabel(" 时");
		//		numericWheelAdapter.setTextSize(15);  设置字体大小
		hour.setViewAdapter(numericWheelAdapter);
		hour.setCyclic(true);
	}

	/**
	 * 初始化分
	 */
	private void initMins() {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 59, "%02d");
		numericWheelAdapter.setLabel(" 分");
//		numericWheelAdapter.setTextSize(15);  设置字体大小
		mins.setViewAdapter(numericWheelAdapter);
		mins.setCyclic(true);
	}


源码下载地址

http://download.csdn.net/detail/qq55214/9471688

你可能感兴趣的:(自定义控件,仿ios时间选择器,WheelView)