仿Iphone的轮子时间选择器(wheel picker)

因为项目中,要要使用这个功能,就抽离出这部分。

activity:

public class Time2Activity extends Activity implements
		android.view.View.OnClickListener {

	private TextView hour;
	private TextView min;
	private TextView daytxt;
	private WheelView hours;
	private WheelView mins;
	private WheelView day;

	private View view;

	private Button timebtn;
	private Builder dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.acy_dialog);
		init();
		// 小时
		hours = (WheelView) view.findViewById(R.id.hour);

		hours.setMyJudge("hour");
		// (min ,max)
		NumericWheelAdapter hourAdapter = new NumericWheelAdapter(this, 0, 23,
				"%02d");
		hourAdapter.setItemResource(R.layout.wheel_text_item);
		hourAdapter.setItemTextResource(R.id.text);

		hours.setViewAdapter(hourAdapter);
		hours.setCyclic(true);

		// 分钟
		mins = (WheelView) view.findViewById(R.id.mins);

		mins.setMyJudge("min");
		// (min ,max ,format)
		NumericWheelAdapter minAdapter = new NumericWheelAdapter(this, 0, 59,
				"%02d");
		minAdapter.setItemResource(R.layout.wheel_text_item);
		minAdapter.setItemTextResource(R.id.text);
		mins.setViewAdapter(minAdapter);
		mins.setCyclic(true);

		// 当前时间
		// set current time
		// Calendar calendar = Calendar.getInstance(Locale.US);
		Calendar calendar = Calendar.getInstance();
		hours.setCurrentItem(calendar.get(Calendar.HOUR));
		mins.setCurrentItem(calendar.get(Calendar.MINUTE));

		day = (WheelView) view.findViewById(R.id.day);
		DayArrayAdapter dayadapter = new DayArrayAdapter(this);
		day.setViewAdapter(dayadapter);

		day.setMyJudge("day");

		/*
		 * 新建对话框,保存轮子时间选择器
		 */
		dialog = new AlertDialog.Builder(this).setTitle("选择时间")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						hour.setText(hours.getHour() + ";小时");
						min.setText(mins.getMin() + ";分鐘");
						daytxt.setText(day.getDay() + ";天數");

						/**
						 * 取得view的父组件,然后移除view
						 */
						((ViewGroup) view.getParent()).removeView(view);
						dialog.dismiss();

					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}

				});
		dialog.setView(view);

	}

	public void init() {
		LayoutInflater inflater = LayoutInflater.from(this);

		view = inflater.inflate(R.layout.time2_layout, null);

		hour = (TextView) findViewById(R.id.textView1);
		min = (TextView) findViewById(R.id.textView2);
		daytxt = (TextView) findViewById(R.id.textView3);

		timebtn = (Button) findViewById(R.id.btn_time);
		timebtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == timebtn) {

			dialog.show();
		}
	}
}


使用AlertDialog来装选择器,不用自定义Dialog那么麻烦。

当前的活动写了3个textview,用呈现你选择了的天数,时 和分钟数目。


效果:

运行效果:

仿Iphone的轮子时间选择器(wheel picker)_第1张图片

对话框效果:

仿Iphone的轮子时间选择器(wheel picker)_第2张图片

对话框消失后的时间:

仿Iphone的轮子时间选择器(wheel picker)_第3张图片


这个工程,我就直接分享到资源里面了,需要的去下载就是了。

点击下载

注意:

1.天数 是指的是离当前日期的 差值,比如是你选的是12月5号,而当前日期是12月3号,那么显示的天数就是之间的差值,2天。你如果想得到当前位置的时间,使用calendar的roll()方法 就可以了。


你可能感兴趣的:(android,开源及第三方项目,对话框,仿Iphone)