package com.bigdo.controls; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.bigdo.app.BaseActivity; import com.bigdo.app.R; import com.bigdo.controls.wheel.widget.OnWheelChangedListener; import com.bigdo.controls.wheel.widget.OnWheelClickedListener; import com.bigdo.controls.wheel.widget.WheelView; import com.bigdo.controls.wheel.widget.adapters.ArrayWheelAdapter; import com.bigdo.controls.wheel.widget.adapters.NumericWheelAdapter; public class DateSelectByYYYYMMDDHHMMSSActivity extends BaseActivity { Calendar calendar; final int minYear = 1000; WheelView month, year, day, hour, minute; @SuppressLint("SimpleDateFormat") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int orientation = this.getIntent().getIntExtra("orientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setRequestedOrientation(orientation); setContentView(R.layout.activity_date_select_yyyymmddhhmm); String date = this.getIntent().getStringExtra("date"); calendar = Calendar.getInstance(); if (date != null && !date.equals("")) { date = date.replace('.', '-'); date = date.replace('/', '-'); if (date != null && date.length() <= 16) { date += ":00"; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date s_date = (Date) sdf.parse(date); calendar.setTime(s_date); } catch (Exception e) { } } month = (WheelView) findViewById(R.id.activity_date_select_yyyymmddhhmm_month); year = (WheelView) findViewById(R.id.activity_date_select_yyyymmddhhmm_year); day = (WheelView) findViewById(R.id.activity_date_select_yyyymmddhhmm_day); hour = (WheelView) findViewById(R.id.activity_date_select_yyyymmddhhmm_hour); minute = (WheelView) findViewById(R.id.activity_date_select_yyyymmddhhmm_minute); Button submit = (Button) findViewById(R.id.activity_date_select_yyyymmddhhmm_submit); OnWheelChangedListener listener = new OnWheelChangedListener() { public void onChanged(WheelView wheel, int oldValue, int newValue) { updateDays(year, month, day); } }; submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { getResult(minYear + year.getCurrentItem(), (1 + month.getCurrentItem()), (1 + (day.getCurrentItem())), hour.getCurrentItem(), minute.getCurrentItem()); } }); // month int curMonth = calendar.get(Calendar.MONTH); String months[] = new String[] { "01月", "02月", "03月", "04月", "05月", "06月", "07月", "08月", "09月", "10月", "11月", "12月" }; month.setViewAdapter(new DateArrayAdapter(this, months, curMonth)); month.setCurrentItem(curMonth); month.setCyclic(true); month.addChangingListener(listener); // year int curYear = calendar.get(Calendar.YEAR); if (curYear < minYear) { curYear = minYear; } else if (curYear > 9999) { curYear = 9998; } year.setViewAdapter(new DateNumericAdapter(this, minYear, 9999, curYear - minYear, "%1$s年")); year.setCurrentItem(curYear - minYear); year.setCyclic(true); year.addChangingListener(listener); int curHour = calendar.get(Calendar.HOUR_OF_DAY); hour.setViewAdapter(new DateNumericAdapter(this, 0, 23, curHour, "%1$02d点")); hour.setCurrentItem(curHour); hour.setCyclic(true); int curMinute = calendar.get(Calendar.MINUTE); minute.setViewAdapter(new DateNumericAdapter(this, 0, 59, curMinute, "%1$02d分")); minute.setCurrentItem(curMinute); minute.setCyclic(true); // day updateDays(year, month, day); day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1); day.setCyclic(true); OnWheelClickedListener click = new OnWheelClickedListener() { public void onItemClicked(WheelView wheel, int itemIndex) { wheel.setCurrentItem(itemIndex, true); } }; year.addClickingListener(click); month.addClickingListener(click); day.addClickingListener(click); hour.addClickingListener(click); minute.addClickingListener(click); } private void getResult(int year, int month, int day, int hour, int minute) { Intent data = new Intent(); data.putExtra("year", year); data.putExtra("month", month); data.putExtra("day", day); data.putExtra("hour", hour); data.putExtra("minute", minute); this.setResult(1, data); this.finish(); } /** * Updates day wheel. Sets max days according to selected month and year */ void updateDays(WheelView year, WheelView month, WheelView day) { calendar.set(Calendar.YEAR, minYear + year.getCurrentItem()); calendar.set(Calendar.MONTH, month.getCurrentItem()); int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar .get(Calendar.DAY_OF_MONTH) - 1, "%1$02d日")); int curDay = Math.min(maxDays, day.getCurrentItem() + 1); day.setCurrentItem(curDay - 1, true); } /** * Adapter for numeric wheels. Highlights the current value. */ private class DateNumericAdapter extends NumericWheelAdapter { // Index of current item int currentItem; // Index of item to be highlighted int currentValue; /** * Constructor */ public DateNumericAdapter(Context context, int minValue, int maxValue, int current, String format) { super(context, minValue, maxValue, format); this.currentValue = current; setTextSize(16); } @Override protected void configureTextView(TextView view) { super.configureTextView(view); if (currentItem == currentValue) { view.setTextColor(0xFF0000F0); } view.setTypeface(Typeface.SANS_SERIF); } @Override public View getItem(int index, View cachedView, ViewGroup parent) { currentItem = index; return super.getItem(index, cachedView, parent); } } /** * Adapter for string based wheel. Highlights the current value. */ private class DateArrayAdapter extends ArrayWheelAdapter<String> { // Index of current item int currentItem; // Index of item to be highlighted int currentValue; /** * Constructor */ public DateArrayAdapter(Context context, String[] items, int current) { super(context, items); this.currentValue = current; setTextSize(16); } @Override protected void configureTextView(TextView view) { super.configureTextView(view); if (currentItem == currentValue) { view.setTextColor(0xFF0000F0); } view.setTypeface(Typeface.SANS_SERIF); } @Override public View getItem(int index, View cachedView, ViewGroup parent) { currentItem = index; return super.getItem(index, cachedView, parent); } } @Override public void onClearData() { // TODO Auto-generated method stub } @Override public void onVideoBroadcast(Intent intent) { // TODO Auto-generated method stub } }