package com.bigdo.controls; import java.util.Calendar; import com.bigdo.app.BaseActivity; import com.bigdo.app.R; 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; 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; public class DateSelectByYYYYMMActivity extends BaseActivity { Calendar calendar; final int minYear = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date_select_yyyymm); int orientation = getIntent().getIntExtra("orientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); this.setRequestedOrientation(orientation); calendar = Calendar.getInstance(); int curYear = getIntent().getIntExtra("year", calendar.get(Calendar.YEAR)); int curMonth = getIntent().getIntExtra("month", calendar.get(Calendar.MARCH) + 1) - 1; final WheelView month = (WheelView) findViewById(R.id.activity_date_select_yyyymm_month); final WheelView year = (WheelView) findViewById(R.id.activity_date_select_yyyymm_year); final Button submit = (Button) findViewById(R.id.activity_date_select_yyyymm_submit); submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { getResult(minYear + year.getCurrentItem(), (1 + month.getCurrentItem())); } }); // month String months[] = new String[] { "01月", "02月", "03月", "04月", "05月", "06月", "07月", "08月", "09月", "10月", "11月", "12月" }; if (curMonth < 0) { curMonth = 0; } else if (curMonth > 12) { curMonth = 11; } month.setViewAdapter(new DateArrayAdapter(this, months, curMonth)); month.setCurrentItem(curMonth); month.setCyclic(true); if (curYear < minYear) { curYear = minYear; } else if (curYear > 9999) { curYear = 9998; } // year year.setViewAdapter(new DateNumericAdapter(this, minYear, 9999, curYear - minYear, "%1$s年")); year.setCurrentItem(curYear - minYear); year.setCyclic(true); OnWheelClickedListener click = new OnWheelClickedListener() { public void onItemClicked(WheelView wheel, int itemIndex) { wheel.setCurrentItem(itemIndex, true); } }; year.addClickingListener(click); month.addClickingListener(click); } private void getResult(int year, int month) { Intent data = new Intent(); data.putExtra("year", year); data.putExtra("month", month); this.setResult(1, data); this.finish(); } /** * 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 } }