1. 程式人生 > >仿Iphone的輪子時間選擇器(wheel picker)

仿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,用呈現你選擇了的天數,時 和分鐘數目。

效果:

執行效果:

對話方塊效果:

對話方塊消失後的時間:


這個工程,我就直接分享到資源裡面了,需要的去下載就是了。

點選下載

注意:

1.天數 是指的是離當前日期的 差值,比如是你選的是12月5號,而當前日期是12月3號,那麼顯示的天數就是之間的差值,2天。你如果想得到當前位置的時間,使用calendar的roll()方法 就可以了。