仿iOS时间选择器和地区选择

时间的效果

iOS中时间选择器 仿ios时间选择器_iOS中时间选择器

地区的效果

iOS中时间选择器 仿ios时间选择器_iOS中时间选择器_02

代码块

代码块语法遵循标准markdown代码,例如:

“` python 
 /** 
 * 蛋疼的时间选择器,算起来好麻烦。。。。。。。 
 * 
 * @author liu time 2015.9.7 
 */ 
 public class TimeActivity extends Activity implements OnClickListener, 
 OnWheelChangedListener { 
 private WheelView mViewProvince; 
 private WheelView mViewCity; 
 private WheelView mViewDistrict; 
 private Button mBtnConfirm;
/**
 * 年
 */
protected List<String> year = new ArrayList<String>();
/**
 * 月
 */
protected String[] moth = new String[] { "1月", "2月", "3月", "4月", "5月",
        "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
/**
 * 日
 */
protected List<String> day;
String year_a;// 每次更新时存储到这个里面
String moth_a;
String day_a;
String year_b;// 这个只记录第一次获取的时间
String moth_b;
String day_b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cityactivity_main);
    Calendar calendar = Calendar.getInstance();
    year_a = year_b = calendar.get(Calendar.YEAR) + "";// 获取当前年
    moth_a = moth_b = calendar.get(Calendar.MONTH) + 1 + "";// 获取当前月
    day_a = day_b = calendar.get(Calendar.DAY_OF_MONTH) + "";// 获取当前日期

    getyear();// 获取年份列表,上一百年到下一百年
    setUpViews();// 实例化控件
    setUpListener();// 为控件添加监听
    setUpData();// 设置
    setTime();
}

/**
 * 将当前的时间添加进时间选择框
 */
private void setTime() {
    int yy = 0;
    int mm = 0;
    int dd = 0;
    for (int i = 0; i < year.size(); i++) {
        if (year.get(i).equals(year_b+"年")) {
            yy = i;
            System.out.println("yy  "+yy);
        }
    }
    for (int i = 0; i < moth.length; i++) {
        if (moth[i].equals(moth_b+"月")) {
            mm = i;
            System.out.println("mm  "+mm);
        }
    }
    for (int i = 0; i < day.size(); i++) {
        if (day.get(i).equals(day_b+"日")) {
            dd = i;
            System.out.println("dd  "+dd);
        }
    }
    mViewProvince.setCurrentItem(yy);
    mViewCity.setCurrentItem(mm);
    mViewDistrict.setCurrentItem(dd);
}

private void setUpViews() {
    mViewProvince = (WheelView) findViewById(R.id.id_province);
    mViewCity = (WheelView) findViewById(R.id.id_city);
    mViewDistrict = (WheelView) findViewById(R.id.id_district);
    mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
    mViewProvince.setCyclic(true);// 设置循环
    mViewCity.setCyclic(true);
    mViewDistrict.setCyclic(true);
}

private void setUpListener() {
    // 添加change事件,年
    mViewProvince.addChangingListener(this);
    // 添加change事件,月
    mViewCity.addChangingListener(this);
    // 添加change事件,日
    mViewDistrict.addChangingListener(this);
    // 添加onclick事件
    mBtnConfirm.setOnClickListener(this);
}

private void setUpData() {
    // 设置可见条目数量
    mViewProvince.setVisibleItems(7);
    mViewCity.setVisibleItems(7);
    mViewDistrict.setVisibleItems(7);
    upyear();
    upmoth();
}

@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
    // TODO Auto-generated method stub
    if (wheel == mViewProvince) {// 滑动年滚轮的事件
        upyear();
    } else if (wheel == mViewCity) {// 滑动月滚轮的事件
        upmoth();
    } else if (wheel == mViewDistrict) {// 滑动日滚轮的事件
        upday();
    }
}

private void upyear() {// 将年份的数据添加进滚轮中
    mViewProvince.setViewAdapter(new ListWheelAdapter<String>(
            TimeActivity.this, year));
    try {
        int pCurrent = mViewProvince.getCurrentItem();
        // 将当前的年赋值给全局
        year_a = year.get(pCurrent);
        // 因为在第一次进入的时候是还没有月份的数据的,所以需要排除掉
        if (moth_a != null) {
            getday(Integer.parseInt(year_a.replace("年", "")),
                    Integer.parseInt(moth_a.replace("月", "")));
        }
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void upmoth() {
    mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(
            TimeActivity.this, moth));
    try {
        int pCurrent = mViewCity.getCurrentItem();
        // 将当前的月赋值给全局
        moth_a = moth[pCurrent];
        getday(Integer.parseInt(year_a.replace("年", "")),
                Integer.parseInt(moth_a.replace("月", "")));
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void upday() {

    try {
        int pCurrent = mViewDistrict.getCurrentItem();
        day_a = day.get(pCurrent);

    } catch (Exception e) {
        mViewDistrict.setCurrentItem(day.size() - 1);
        day_a = day.get(day.size() - 1);
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_confirm:
        showSelectedResult();
        break;
    default:
        break;
    }
}

private void showSelectedResult() {
    Toast.makeText(TimeActivity.this, "当前选中:" + year_a + moth_a + day_a,
            Toast.LENGTH_SHORT).show();
}

/**
 * 获取上下两百年的年份信息
 */
private void getyear() {
    int aa = Integer.parseInt(year_a) - 100;
    for (int i = 0; i < 100; i++) {
        year.add((aa + i) + "" + "年");
    }
    for (int i = 0; i < 100; i++) {
        year.add((Integer.parseInt(year_a) + i) + "" + "年");
    }
}

/**
 * 根据当前的年月来获取日的信息
 * 
 * @param year
 * @param month
 */
private void getday(int year, int month) {
    day = new ArrayList<String>();
    Calendar a = Calendar.getInstance();
    a.set(Calendar.YEAR, year);
    a.set(Calendar.MONTH, month);
    a.set(Calendar.DATE, 1);
    a.set(Calendar.DATE, -1);
    int maxdate = a.get(Calendar.DATE);
    for (int i = 1; i <= maxdate + 1; i++) {
        day.add(i + "日");
    }
    mViewDistrict.setViewAdapter(new ListWheelAdapter<String>(
            TimeActivity.this, day));
    upday();
}
}
/** 
 * 仿照iOS的滚动地区选择器 
 * 
 */public class CityActivity extends BaseActivity implements OnClickListener, 
 OnWheelChangedListener { 
 private WheelView mViewProvince; 
 private WheelView mViewCity; 
 private WheelView mViewDistrict; 
 private Button mBtnConfirm; 
 private TextView nn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cityactivity_main);
    setUpViews();
    setUpListener();
    setUpData();
    nn = (TextView)this.findViewById(R.id.nn);
    nn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(CityActivity.this, TimeActivity.class);
            startActivity(intent);
        }
    });
}

private void setUpViews() {
    mViewProvince = (WheelView) findViewById(R.id.id_province);
    mViewCity = (WheelView) findViewById(R.id.id_city);
    mViewDistrict = (WheelView) findViewById(R.id.id_district);
    mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
}

private void setUpListener() {
    // 添加change事件,省
    mViewProvince.addChangingListener(this);
    // 添加change事件,市
    mViewCity.addChangingListener(this);
    // 添加change事件,区
    mViewDistrict.addChangingListener(this);
    // 添加onclick事件
    mBtnConfirm.setOnClickListener(this);
}

private void setUpData() {
    initProvinceDatas();
    mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(
            CityActivity.this, mProvinceDatas));
    // 设置可见条目数量
    mViewProvince.setVisibleItems(7);
    mViewCity.setVisibleItems(7);
    mViewDistrict.setVisibleItems(7);
    updateCities();
    updateAreas();
}

@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
    // TODO Auto-generated method stub
    if (wheel == mViewProvince) {
        updateCities();
    } else if (wheel == mViewCity) {
        updateAreas();
    } else if (wheel == mViewDistrict) {
        updateZipCode();
    }
}

/**
 * 根据当前的省,更新市WheelView的信息
 */
private void updateCities() {
    try {
        int pCurrent = mViewProvince.getCurrentItem();
        // 将当前的省赋值给全局
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[] { "" };
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 * 根据当前的市,更新区WheelView的信息
 */
private void updateAreas() {
    try {
        int pCurrent = mViewCity.getCurrentItem();
        // 将当前的市赋值给全局
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[] { "" };
        }
        mViewDistrict
                .setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
        mViewDistrict.setCurrentItem(0);
        updateZipCode();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/**
 * 将变化的区和邮政编码更新到全局
 * 
 * @param v
 */
private void updateZipCode() {
    try {
        int pCurrent = mViewDistrict.getCurrentItem();
        mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[pCurrent];
        mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_confirm:
        showSelectedResult();
        break;
    default:
        break;
    }
}

private void showSelectedResult() {
    Toast.makeText(
            CityActivity.this,
            "当前选中:" + mCurrentProviceName + "," + mCurrentCityName + ","
                    + mCurrentDistrictName + "," + mCurrentZipCode,
            Toast.LENGTH_SHORT).show();
}

}

这是根据一位前辈写的代码修改而来的,原来的博客链接我找不到了,如果前辈看见的话联系我 我会将您的连接添加上去。我是新手如果哪里写的不尽人意,尽管提出意见,谢谢