最近项目里面用到了一个日期选择器,实现现在主流的WheelView滑动选择,整理了下,做了个Demo.废话不多说,直接上代码.
主布局:activity_main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
MainActivity:
package com.example.user.mydate; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.Window; import android.widget.TextView; import android.widget.Toast; import com.example.user.mydate.view.ChangeDatePopwindow; public class MainActivity extends Activity { private TextView selectDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); selectDate= (TextView) findViewById(R.id.selectDate); } public void selectDate(View v){ if (v.getId()==R.id.selectDate){ selectDate(); } } private String[] selectDate() { final String[] str = new String[10]; ChangeDatePopwindow mChangeBirthDialog = new ChangeDatePopwindow( this); mChangeBirthDialog.setDate("2016", "1", "1"); mChangeBirthDialog.showAtLocation(selectDate, Gravity.BOTTOM, 0, 0); mChangeBirthDialog.setBirthdayListener(new ChangeDatePopwindow.OnBirthListener() { @Override public void onClick(String year, String month, String day) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,year + "-" + month + "-" + day,Toast.LENGTH_LONG).show(); StringBuilder sb = new StringBuilder(); sb.append(year.substring(0, year.length() - 1)).append("-").append(month.substring(0, day.length() - 1)).append("-").append(day); str[0] = year + "-" + month + "-" + day; str[1] = sb.toString(); selectDate.setText(year + "-" + month + "-" + day); } }); return str; } }
主要的类:
package com.example.user.mydate.view; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.PopupWindow; import android.widget.TextView; import com.example.user.mydate.R; import com.example.user.mydate.view.wheelview.OnWheelChangedListener; import com.example.user.mydate.view.wheelview.OnWheelScrollListener; import com.example.user.mydate.view.wheelview.WheelView; import com.example.user.mydate.view.wheelview.adapter.AbstractWheelTextAdapter1; import java.util.ArrayList; import java.util.Calendar; /** * Author: Chen.yuan * Email: hubeiqiyuan2010@163.com * Date: 2016/7/28 17:37 * Description:日期选择window */ public class ChangeDatePopwindow extends PopupWindow implements View.OnClickListener { private Context context; private WheelView wvYear; private WheelView wvMonth; private WheelView wvDay; private TextView btnSure; private TextView btnCancel; private ArrayListarry_years = new ArrayList (); private ArrayList arry_mOnths= new ArrayList (); private ArrayList arry_days = new ArrayList (); private CalendarTextAdapter mYearAdapter; private CalendarTextAdapter mMonthAdapter; private CalendarTextAdapter mDaydapter; private String month; private String day; private String currentYear = getYear(); private String currentMOnth= getMonth(); private String currentDay = getDay(); private int maxTextSize = 24; private int minTextSize = 14; private boolean issetdata = false; private String selectYear; private String selectMonth; private String selectDay; private OnBirthListener onBirthListener; public ChangeDatePopwindow(final Context context) { super(context); this.cOntext= context; View view=View.inflate(context, R.layout.dialog_myinfo_changebirth,null); wvYear = (WheelView) view.findViewById(R.id.wv_birth_year); wvMOnth= (WheelView) view.findViewById(R.id.wv_birth_month); wvDay = (WheelView) view.findViewById(R.id.wv_birth_day); btnSure = (TextView) view.findViewById(R.id.btn_myinfo_sure); btnCancel = (TextView) view.findViewById(R.id.btn_myinfo_cancel); //设置SelectPicPopupWindow的View this.setContentView(view); //设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置SelectPicPopupWindow弹出窗体的高 this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); //设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); //设置SelectPicPopupWindow弹出窗体动画效果 // this.setAnimationStyle(R.style.AnimBottom); //实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); //设置SelectPicPopupWindow弹出窗体的背景 this.setBackgroundDrawable(dw); btnSure.setOnClickListener(this); btnCancel.setOnClickListener(this); if (!issetdata) { initData(); } initYears(); mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize); wvYear.setVisibleItems(5); wvYear.setViewAdapter(mYearAdapter); wvYear.setCurrentItem(setYear(currentYear)); initMonths(Integer.parseInt(month)); mMOnthAdapter= new CalendarTextAdapter(context, arry_months, setMonth(currentMonth), maxTextSize, minTextSize); wvMonth.setVisibleItems(5); wvMonth.setViewAdapter(mMonthAdapter); wvMonth.setCurrentItem(setMonth(currentMonth)); initDays(Integer.parseInt(day)); mDaydapter = new CalendarTextAdapter(context, arry_days, Integer.parseInt(currentDay) - 1, maxTextSize, minTextSize); wvDay.setVisibleItems(5); wvDay.setViewAdapter(mDaydapter); wvDay.setCurrentItem(Integer.parseInt(currentDay) - 1); wvYear.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); selectYear = currentText; setTextviewSize(currentText, mYearAdapter); currentYear = currentText.substring(0, currentText.length()-1).toString(); Log.d("currentYear==",currentYear); setYear(currentYear); initMonths(Integer.parseInt(month)); mMOnthAdapter= new CalendarTextAdapter(context, arry_months, 0, maxTextSize, minTextSize); wvMonth.setVisibleItems(5); wvMonth.setViewAdapter(mMonthAdapter); wvMonth.setCurrentItem(0); calDays(currentYear, month); } }); wvYear.addScrollingListener(new OnWheelScrollListener() { @Override public void onScrollingStarted(WheelView wheel) { // TODO Auto-generated method stub } @Override public void onScrollingFinished(WheelView wheel) { // TODO Auto-generated method stub String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); setTextviewSize(currentText, mYearAdapter); } }); wvMonth.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); selectMOnth= currentText; setTextviewSize(currentText, mMonthAdapter); setMonth(currentText.substring(0, 1)); initDays(Integer.parseInt(day)); mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize); wvDay.setVisibleItems(5); wvDay.setViewAdapter(mDaydapter); wvDay.setCurrentItem(0); calDays(currentYear, month); } }); wvMonth.addScrollingListener(new OnWheelScrollListener() { @Override public void onScrollingStarted(WheelView wheel) { // TODO Auto-generated method stub } @Override public void onScrollingFinished(WheelView wheel) { // TODO Auto-generated method stub String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); setTextviewSize(currentText, mMonthAdapter); } }); wvDay.addChangingListener(new OnWheelChangedListener() { @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); setTextviewSize(currentText, mDaydapter); selectDay = currentText; } }); wvDay.addScrollingListener(new OnWheelScrollListener() { @Override public void onScrollingStarted(WheelView wheel) { // TODO Auto-generated method stub } @Override public void onScrollingFinished(WheelView wheel) { // TODO Auto-generated method stub String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); setTextviewSize(currentText, mDaydapter); } }); } public void initYears() { for (int i = Integer.parseInt(getYear()); i > 1950; i--) { arry_years.add(i + "年"); } } public void initMonths(int months) { arry_months.clear(); for (int i = 1; i <= months; i++) { arry_months.add(i + "月"); } } public void initDays(int days) { arry_days.clear(); for (int i = 1; i <= days; i++) { arry_days.add(i + "日"); } } private class CalendarTextAdapter extends AbstractWheelTextAdapter1 { ArrayList list; protected CalendarTextAdapter(Context context, ArrayList list, int currentItem, int maxsize, int minsize) { super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize); this.list = list; setItemTextResource(R.id.tempValue); } @Override public View getItem(int index, View cachedView, ViewGroup parent) { View view = super.getItem(index, cachedView, parent); return view; } @Override public int getItemsCount() { return list.size(); } @Override protected CharSequence getItemText(int index) { return list.get(index) + ""; } } public void setBirthdayListener(OnBirthListener onBirthListener) { this.OnBirthListener= onBirthListener; } @Override public void onClick(View v) { if (v == btnSure) { if (onBirthListener != null) { onBirthListener.onClick(selectYear, selectMonth, selectDay); Log.d("cy",""+selectYear+""+selectMonth+""+selectDay); } } else if (v == btnSure) { } else { dismiss(); } dismiss(); } public interface OnBirthListener { public void onClick(String year, String month, String day); } /** * 设置字体大小 * * @param curriteItemText * @param adapter */ public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) { ArrayList arrayList = adapter.getTestViews(); int size = arrayList.size(); String currentText; for (int i = 0; i 1950; i--) { if (i == Integer.parseInt(year)) { return yearIndex; } yearIndex++; } return yearIndex; } /** * 设置月份 * * @param month * @param month * @return */ public int setMonth(String month) { int mOnthIndex= 0; calDays(currentYear, month); for (int i = 1; i
底部弹出框布局:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
运行效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。