基于WheelView自定义的DatePickerDialog

本人利用WheelView写的一个DatePickerDialog 
(还有一个TimePickerDialog,本人忘了在写在哪个项目里了,等找到了也贴上来) 

先看图,有个直观的了解 


DatePickerDialog代码: 

Java代码   收藏代码
  1. import java.util.Calendar;  
  2.   
  3. import com.widget.wheel.NumericWheelAdapter;  
  4. import com.widget.wheel.OnWheelScrollListener;  
  5. import com.widget.wheel.WheelView;  
  6. import com.yirui.youbao.app.R;  
  7.   
  8. import android.app.Dialog;  
  9. import android.content.Context;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. /** 
  15.  *  
  16.  * @author pythoner 
  17.  *  
  18.  */  
  19. public class DatePickerDialog extends Dialog  
  20. {  
  21.   
  22.     private Button btn_left, btn_right;  
  23.   
  24.     private WheelView year, month, day;  
  25.   
  26.     private String date;//初始化显示的日期,默认为当日  
  27.   
  28.     public DatePickerDialog(Context context, String date)  
  29.     {  
  30. //        super(context);  
  31.         this(context, R.style.Theme_Dialog_NoTitle, date);  
  32.         // TODO Auto-generated constructor stub  
  33.     }  
  34.   
  35.     public DatePickerDialog(Context context, int theme, String date)  
  36.     {  
  37.         super(context, theme);  
  38.         // TODO Auto-generated constructor stub  
  39.         this.date = date;  
  40.         init();  
  41.     }  
  42.   
  43.     private void init()  
  44.     {  
  45.         this.setCanceledOnTouchOutside(true);  
  46.         this.setCancelable(true);  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onCreate(Bundle savedInstanceState)  
  51.     {  
  52.         // TODO Auto-generated method stub  
  53.         super.onCreate(savedInstanceState);  
  54.         setContentView(R.layout.dialog_date_picker);  
  55.   
  56.         initViews();  
  57.         initValues();  
  58.     }  
  59.   
  60.     private void initViews()  
  61.     {  
  62.         btn_left = (Button) findViewById(R.id.btn_left);  
  63.         btn_left.setOnClickListener(clickListener);  
  64.         btn_right = (Button) findViewById(R.id.btn_right);  
  65.         btn_right.setOnClickListener(clickListener);  
  66.   
  67.         String icurYear, icurMonth, icurDay;  
  68.         Calendar c = Calendar.getInstance();  
  69.         int curYear = c.get(Calendar.YEAR);  
  70.         if (date == null || date.length() < 10)  
  71.         {// 格式不正确  
  72.             int curMonth = c.get(Calendar.MONTH);  
  73.             int curDay = c.get(Calendar.DAY_OF_MONTH);  
  74.             icurYear = String.valueOf(curYear);  
  75.             icurMonth = String.valueOf(curMonth + 1);  
  76.             icurDay = String.valueOf(curDay);  
  77.         }  
  78.         else  
  79.         {// 年月日  
  80.             icurYear = date.substring(04);  
  81.             icurMonth = date.substring(57);  
  82.             icurDay = date.substring(810);  
  83.         }  
  84.   
  85.         year = (WheelView) findViewById(R.id.year);  
  86.         year.setAdapter(new NumericWheelAdapter(curYear - 2, curYear));  
  87.         // year.setLabel("年");  
  88.         year.setCurrentItem(Integer.parseInt(icurYear) - Integer.parseInt(year.getAdapter().getItem(0)));  
  89.   
  90.         month = (WheelView) findViewById(R.id.month);  
  91.         month.setAdapter(new NumericWheelAdapter(112));// "%02d"  
  92.         // month.setLabel("月");  
  93.         month.setCyclic(true);  
  94.         month.setCurrentItem(Integer.parseInt(icurMonth) - 1);  
  95.   
  96.         int daysOfMounth = getDaysOfMounth();  
  97.         day = (WheelView) findViewById(R.id.day);  
  98.         day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));  
  99.         // day.setLabel("日");  
  100.         day.setCyclic(true);  
  101.         day.setCurrentItem(Integer.parseInt(icurDay) - 1);  
  102.   
  103.         OnWheelScrollListener scrollListener = new OnWheelScrollListener()  
  104.         {  
  105.             public void onScrollingStarted(WheelView wheel)  
  106.             {  
  107.             }  
  108.   
  109.             public void onScrollingFinished(WheelView wheel)  
  110.             {  
  111.                 int daysOfMounth = getDaysOfMounth();  
  112.                 day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));  
  113.             }  
  114.         };  
  115.   
  116.         year.addScrollingListener(scrollListener);  
  117.         month.addScrollingListener(scrollListener);  
  118.         // day.addScrollingListener(scrollListener);  
  119.   
  120.     }  
  121.   
  122.     private int getDaysOfMounth()  
  123.     {  
  124.         int mMonth = Integer.parseInt(month.getAdapter().getItem(month.getCurrentItem()));  
  125.         switch (mMonth)  
  126.         {  
  127.             case 1:  
  128.             case 3:  
  129.             case 5:  
  130.             case 7:  
  131.             case 8:  
  132.             case 10:  
  133.             case 12:  
  134.                 return 31;  
  135.             case 4:  
  136.             case 6:  
  137.             case 9:  
  138.             case 11:  
  139.                 return 30;  
  140.             case 2:  
  141.                 int mYear = Integer.parseInt(year.getAdapter().getItem(year.getCurrentItem()));  
  142.                 if (mYear % 4 == 0 && mYear % 100 != 0 || mYear % 400 == 0)  
  143.                 {  
  144.                     return 29;  
  145.                 }  
  146.                 else  
  147.                 {  
  148.                     return 28;  
  149.                 }  
  150.         }  
  151.         return -1;  
  152.     }  
  153.   
  154.     private void initValues()  
  155.     {  
  156.   
  157.     }  
  158.   
  159.     View.OnClickListener clickListener = new View.OnClickListener()  
  160.     {  
  161.   
  162.         @Override  
  163.         public void onClick(View v)  
  164.         {  
  165.             // TODO Auto-generated method stub  
  166.             switch (v.getId())  
  167.             {  
  168.                 case R.id.btn_left:  
  169.                     dismiss();  
  170.                     break;  
  171.                 case R.id.btn_right:  
  172.                     String mYear = year.getAdapter().getItem(year.getCurrentItem());  
  173.                     String mMonth = month.getAdapter().getItem(month.getCurrentItem());  
  174.                     String mDay = day.getAdapter().getItem(day.getCurrentItem());  
  175.                     if (mDay == null || mDay.length() == 0)  
  176.                     {  
  177.                         mDay = "1";  
  178.                     }  
  179.                     if (Integer.parseInt(mMonth) < 10)  
  180.                     {  
  181.                         mMonth = "0" + mMonth;  
  182.                     }  
  183.                     if (Integer.parseInt(mDay) < 10)  
  184.                     {  
  185.                         mDay = "0" + mDay;  
  186.                     }  
  187.                     if (onOKClickListener != null)  
  188.                     {  
  189.                         onOKClickListener.onOKClick(mYear, mMonth, mDay);  
  190.                     }  
  191.                     dismiss();  
  192.                     break;  
  193.   
  194.                 default:  
  195.                     break;  
  196.             }  
  197.         }  
  198.   
  199.     };  
  200.   
  201.     private OnOKClickListener onOKClickListener;  
  202.   
  203.     public interface OnOKClickListener  
  204.     {  
  205.         public void onOKClick(String year, String month, String date);  
  206.     }  
  207.   
  208.     public void setOnOKClickListener(OnOKClickListener onOKClickListener)  
  209.     {  
  210.         this.onOKClickListener = onOKClickListener;  
  211.     }  
  212. }  


DatePickerDialog的布局文件: 
Xml代码   收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="250dp"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical"   
  5.     android:gravity="center"  
  6.     android:background="@drawable/bg_picker_dialog"  
  7.     >  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:padding="8dp"   
  14.         >  
  15.   
  16.         <LinearLayout  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="match_parent"  
  19.             android:orientation="vertical"   
  20.             android:layout_weight="1"   
  21.             android:gravity="center_horizontal"  
  22.             >  
  23.   
  24.             <ImageView  
  25.                 android:layout_width="wrap_content"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:layout_gravity="center_horizontal"  
  28.                 android:scaleType="fitCenter"  
  29.                 />  
  30.   
  31.             <com.widget.wheel.WheelView  
  32.                 android:id="@+id/year"  
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="match_parent"  
  35.                 android:layout_marginBottom="10dip"  
  36.                 android:layout_marginTop="10dip"  
  37.                 android:layout_weight="1"   
  38.                 />  
  39.   
  40.             <ImageView  
  41.                 android:layout_width="wrap_content"  
  42.                 android:layout_height="wrap_content"  
  43.                 android:layout_gravity="center_horizontal"  
  44.                 android:scaleType="fitCenter"  
  45.                 />  
  46.         </LinearLayout>  
  47.   
  48.         <LinearLayout  
  49.             android:layout_width="match_parent"  
  50.             android:layout_height="match_parent"  
  51.             android:orientation="vertical"   
  52.             android:layout_weight="1"   
  53.             android:gravity="center_horizontal"  
  54.             >  
  55.   
  56.             <ImageView  
  57.                 android:layout_width="wrap_content"  
  58.                 android:layout_height="wrap_content"  
  59.                 android:layout_gravity="center_horizontal"  
  60.                 android:scaleType="fitCenter"  
  61.                 />  
  62.   
  63.             <com.widget.wheel.WheelView  
  64.                 android:id="@+id/month"  
  65.                 android:layout_width="wrap_content"  
  66.                 android:layout_height="match_parent"  
  67.                 android:layout_marginBottom="8dip"  
  68.                 android:layout_marginTop="8dip"  
  69.                 android:layout_weight="1" />  
  70.   
  71.             <ImageView  
  72.                 android:layout_width="wrap_content"  
  73.                 android:layout_height="wrap_content"  
  74.                 android:layout_gravity="center_horizontal"  
  75.                 android:scaleType="fitCenter"  
  76.                 />  
  77.         </LinearLayout>  
  78.           
  79.         <LinearLayout  
  80.             android:layout_width="match_parent"  
  81.             android:layout_height="match_parent"  
  82.             android:orientation="vertical"   
  83.             android:layout_weight="1"   
  84.             android:gravity="center_horizontal"  
  85.             >  
  86.   
  87.             <ImageView  
  88.                 android:layout_width="wrap_content"  
  89.                 android:layout_height="wrap_content"  
  90.                 android:layout_gravity="center_horizontal"  
  91.                 android:scaleType="fitCenter"  
  92.                 />  
  93.   
  94.             <com.widget.wheel.WheelView  
  95.                 android:id="@+id/day"  
  96.                 android:layout_width="wrap_content"  
  97.                 android:layout_height="match_parent"  
  98.                 android:layout_marginBottom="8dip"  
  99.                 android:layout_marginTop="8dip"  
  100.                 android:layout_weight="1" />  
  101.   
  102.             <ImageView  
  103.                 android:layout_width="wrap_content"  
  104.                 android:layout_height="wrap_content"  
  105.                 android:layout_gravity="center_horizontal"  
  106.                 android:scaleType="fitCenter"  
  107.                 />  
  108.         </LinearLayout>  
  109.           
  110.     </LinearLayout>  
  111.   
  112.   
  113.     <LinearLayout  
  114.         android:layout_width="match_parent"  
  115.         android:layout_height="wrap_content"   
  116.         android:orientation="horizontal"  
  117.         android:background="@drawable/line_top_with_blue"  
  118.         >  
  119.   
  120.         <Button  
  121.             android:id="@+id/btn_left"  
  122.             android:layout_width="match_parent"  
  123.             android:layout_height="wrap_content"  
  124.             android:layout_weight="1"  
  125.             android:paddingBottom="8dip"  
  126.             android:paddingTop="8dip"  
  127.             android:text="@string/cancel"  
  128.             android:textColor="@android:color/black"  
  129.             android:textSize="@dimen/font_big"   
  130.             android:background="@drawable/bg_btn_trans_blue_with_no_corner"  
  131.             />  
  132.   
  133.   
  134.         <View   
  135.             android:layout_width="@dimen/line_width"  
  136.             android:layout_height="match_parent"  
  137.             android:background="@color/primary"  
  138.             />  
  139.           
  140.         <Button  
  141.             android:id="@+id/btn_right"  
  142.             android:layout_width="match_parent"  
  143.             android:layout_height="wrap_content"  
  144.             android:layout_weight="1"  
  145.             android:paddingBottom="8dip"  
  146.             android:paddingTop="8dip"  
  147.             android:text="@string/confirm"  
  148.             android:textColor="@android:color/black"  
  149.             android:textSize="@dimen/font_big"   
  150.             android:background="@drawable/bg_btn_trans_blue_with_no_corner"  
  151.             />  
  152.     </LinearLayout>  
  153.   
  154. </LinearLayout>  


style: 
Xml代码   收藏代码
  1. <style name="Theme_Dialog_NoTitle" parent="@android:style/Theme.Dialog">  
  2.         <item name="android:windowFrame">@null</item>  
  3.         <item name="android:windowNoTitle">true</item>  
  4.         <item name="android:windowIsFloating">true</item>  
  5.         <item name="android:windowIsTranslucent">true</item>  
  6.         <item name="android:backgroundDimEnabled">true</item>  
  7.         <item name="android:windowBackground">@android:color/transparent</item>  
  8.     </style>  



使用: 
Java代码   收藏代码
  1. dialog = new DatePickerDialog(context, R.style.Theme_Dialog, btn_startDate.getText().toString().trim());  
  2.             dialog.setOnOKClickListener(new DatePickerDialog.OnOKClickListener()  
  3.             {  
  4.   
  5.                 @Override  
  6.                 public void onOKClick(String year, String month, String date)  
  7.                 {  
  8.                     // TODO Auto-generated method stub  
  9.                     //dosomething  
  10.                     //btn_startDate.setText(year + "-" + month + "-" + date);  
  11.                      
  12.                 }  
  13.             });  
  14.             dialog.show();  

转载:http://gundumw100.iteye.com/blog/2220784

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值