来来来,既然是对话框,我们肯定的继承对话框,废话不多说,直接上代码
1 package com.egojit.android.pspsp.app.widget.views; 2 3 import android.app.Dialog; 4 import android.content.Context; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 11 import com.egojit.android.pspsp.R; 12 import com.egojit.android.pspsp.app.widget.adapter.AbstractWheelTextAdapter; 13 14 import java.util.ArrayList; 15 import java.util.Calendar; 16 17 /** 18 * 日期选择对话框 19 * 20 * @author ywl 21 * 22 */ 23 public class MyDatePickerDialog extends Dialog implements View.OnClickListener { 24 25 private Context context; 26 private WheelView wvYear; 27 private WheelView wvMonth; 28 private WheelView wvDay; 29 30 private View vChangeBirth; 31 private View vChangeBirthChild; 32 private TextView btnSure; 33 private TextView btnCancel; 34 private boolean isHasDay=true;//是否有天 35 36 private ArrayList<String> arry_years = new ArrayList<String>(); 37 private ArrayList<String> arry_months = new ArrayList<String>(); 38 private ArrayList<String> arry_days = new ArrayList<String>(); 39 private CalendarTextAdapter mYearAdapter; 40 private CalendarTextAdapter mMonthAdapter; 41 private CalendarTextAdapter mDaydapter; 42 43 private int month; 44 private int day; 45 46 private int currentYear = getYear(); 47 private int currentMonth = 1; 48 private int currentDay = 1; 49 50 private int maxTextSize = 24; 51 private int minTextSize = 14; 52 53 private boolean issetdata = false; 54 55 private String selectYear; 56 private String selectMonth; 57 private String selectDay; 58 59 private OnBirthListener onBirthListener; 60 61 public MyDatePickerDialog(Context context,boolean isHasDay) { 62 super(context, R.style.ShareDialog); 63 this.context = context; 64 this.isHasDay=isHasDay; 65 } 66 67 @Override 68 protected void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 setContentView(R.layout.dialog_myinfo_changebirth); 71 wvYear = (WheelView) findViewById(R.id.wv_birth_year); 72 wvMonth = (WheelView) findViewById(R.id.wv_birth_month); 73 wvDay = (WheelView) findViewById(R.id.wv_birth_day); 74 75 vChangeBirth = findViewById(R.id.ly_myinfo_changebirth); 76 vChangeBirthChild = findViewById(R.id.ly_myinfo_changebirth_child); 77 btnSure = (TextView) findViewById(R.id.btn_myinfo_sure); 78 btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel); 79 80 vChangeBirth.setOnClickListener(this); 81 vChangeBirthChild.setOnClickListener(this); 82 btnSure.setOnClickListener(this); 83 btnCancel.setOnClickListener(this); 84 85 if (!issetdata) { 86 initData(); 87 } 88 initYears(); 89 mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize); 90 wvYear.setVisibleItems(5); 91 wvYear.setViewAdapter(mYearAdapter); 92 wvYear.setCurrentItem(setYear(currentYear)); 93 94 initMonths(month); 95 mMonthAdapter = new CalendarTextAdapter(context, arry_months, setMonth(currentMonth), maxTextSize, minTextSize); 96 wvMonth.setVisibleItems(5); 97 wvMonth.setViewAdapter(mMonthAdapter); 98 wvMonth.setCurrentItem(setMonth(currentMonth)); 99 100 if(isHasDay){ 101 initDays(day); 102 }else { 103 wvDay.setVisibility(View.GONE); 104 } 105 106 107 108 wvYear.addChangingListener(new OnWheelChangedListener() { 109 110 @Override 111 public void onChanged(WheelView wheel, int oldValue, int newValue) { 112 // TODO Auto-generated method stub 113 String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); 114 selectYear = currentText; 115 setTextviewSize(currentText, mYearAdapter); 116 currentYear = Integer.parseInt(currentText); 117 setYear(currentYear); 118 initMonths(month); 119 mMonthAdapter = new CalendarTextAdapter(context, arry_months, 0, maxTextSize, minTextSize); 120 wvMonth.setVisibleItems(5); 121 wvMonth.setViewAdapter(mMonthAdapter); 122 wvMonth.setCurrentItem(0); 123 } 124 }); 125 126 wvYear.addScrollingListener(new OnWheelScrollListener() { 127 128 @Override 129 public void onScrollingStarted(WheelView wheel) { 130 // TODO Auto-generated method stub 131 132 } 133 134 @Override 135 public void onScrollingFinished(WheelView wheel) { 136 // TODO Auto-generated method stub 137 String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); 138 setTextviewSize(currentText, mYearAdapter); 139 } 140 }); 141 142 wvMonth.addChangingListener(new OnWheelChangedListener() { 143 144 @Override 145 public void onChanged(WheelView wheel, int oldValue, int newValue) { 146 // TODO Auto-generated method stub 147 String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); 148 selectMonth = currentText; 149 setTextviewSize(currentText, mMonthAdapter); 150 setMonth(Integer.parseInt(currentText)); 151 if(isHasDay) { 152 initDays(day); 153 mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize); 154 wvDay.setVisibleItems(5); 155 wvDay.setViewAdapter(mDaydapter); 156 wvDay.setCurrentItem(0); 157 } 158 } 159 }); 160 161 wvMonth.addScrollingListener(new OnWheelScrollListener() { 162 163 @Override 164 public void onScrollingStarted(WheelView wheel) { 165 // TODO Auto-generated method stub 166 167 } 168 169 @Override 170 public void onScrollingFinished(WheelView wheel) { 171 // TODO Auto-generated method stub 172 String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); 173 setTextviewSize(currentText, mMonthAdapter); 174 } 175 }); 176 177 178 179 } 180 181 public void initYears() { 182 for (int i = getYear(); i > 1950; i--) { 183 arry_years.add(i + ""); 184 } 185 } 186 187 public void initMonths(int months) { 188 arry_months.clear(); 189 for (int i = 1; i <= months; i++) { 190 arry_months.add(i + ""); 191 } 192 } 193 194 public void initDays(int days) { 195 arry_days.clear(); 196 wvDay.setVisibility(View.VISIBLE); 197 for (int i = 1; i <= days; i++) { 198 arry_days.add(i + ""); 199 200 } 201 mDaydapter = new CalendarTextAdapter(context, arry_days, currentDay - 1, maxTextSize, minTextSize); 202 203 wvDay.setVisibleItems(5); 204 wvDay.setViewAdapter(mDaydapter); 205 wvDay.setCurrentItem(currentDay - 1); 206 wvDay.addChangingListener(new OnWheelChangedListener() { 207 208 @Override 209 public void onChanged(WheelView wheel, int oldValue, int newValue) { 210 // TODO Auto-generated method stub 211 String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); 212 setTextviewSize(currentText, mDaydapter); 213 selectDay = currentText; 214 } 215 }); 216 217 wvDay.addScrollingListener(new OnWheelScrollListener() { 218 219 @Override 220 public void onScrollingStarted(WheelView wheel) { 221 // TODO Auto-generated method stub 222 223 } 224 225 @Override 226 public void onScrollingFinished(WheelView wheel) { 227 // TODO Auto-generated method stub 228 String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); 229 setTextviewSize(currentText, mDaydapter); 230 } 231 }); 232 } 233 234 private class CalendarTextAdapter extends AbstractWheelTextAdapter { 235 ArrayList<String> list; 236 237 protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) { 238 super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize); 239 this.list = list; 240 setItemTextResource(R.id.tempValue); 241 } 242 243 @Override 244 public View getItem(int index, View cachedView, ViewGroup parent) { 245 View view = super.getItem(index, cachedView, parent); 246 return view; 247 } 248 249 @Override 250 public int getItemsCount() { 251 return list.size(); 252 } 253 254 @Override 255 protected CharSequence getItemText(int index) { 256 return list.get(index) + ""; 257 } 258 } 259 260 public void setBirthdayListener(OnBirthListener onBirthListener) { 261 this.onBirthListener = onBirthListener; 262 } 263 264 @Override 265 public void onClick(View v) { 266 267 if (v == btnSure) { 268 if (onBirthListener != null) { 269 onBirthListener.onClick(selectYear, selectMonth, selectDay); 270 } 271 } else if (v == btnSure) { 272 273 } else if (v == vChangeBirthChild) { 274 return; 275 } else { 276 dismiss(); 277 } 278 dismiss(); 279 280 } 281 282 public interface OnBirthListener { 283 public void onClick(String year, String month, String day); 284 } 285 286 /** 287 * 设置字体大小 288 * 289 * @param curriteItemText 290 * @param adapter 291 */ 292 public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) { 293 ArrayList<View> arrayList = adapter.getTestViews(); 294 int size = arrayList.size(); 295 String currentText; 296 for (int i = 0; i < size; i++) { 297 TextView textvew = (TextView) arrayList.get(i); 298 currentText = textvew.getText().toString(); 299 if (curriteItemText.equals(currentText)) { 300 textvew.setTextSize(maxTextSize); 301 } else { 302 textvew.setTextSize(minTextSize); 303 } 304 } 305 } 306 307 public int getYear() { 308 Calendar c = Calendar.getInstance(); 309 return c.get(Calendar.YEAR); 310 } 311 312 public int getMonth() { 313 Calendar c = Calendar.getInstance(); 314 return c.get(Calendar.MONTH) + 1; 315 } 316 317 public int getDay() { 318 Calendar c = Calendar.getInstance(); 319 return c.get(Calendar.DATE); 320 } 321 322 public void initData() { 323 setDate(getYear(), getMonth(), getDay()); 324 this.currentDay = 1; 325 this.currentMonth = 1; 326 } 327 328 /** 329 * 设置年月日 330 * 331 * @param year 332 * @param month 333 * @param day 334 */ 335 public void setDate(int year, int month, int day) { 336 selectYear = year + ""; 337 selectMonth = month + ""; 338 selectDay = day + ""; 339 issetdata = true; 340 this.currentYear = year; 341 this.currentMonth = month; 342 this.currentDay = day; 343 if (year == getYear()) { 344 this.month = getMonth(); 345 } else { 346 this.month = 12; 347 } 348 calDays(year, month); 349 } 350 351 /** 352 * 设置年份 353 * 354 * @param year 355 */ 356 public int setYear(int year) { 357 int yearIndex = 0; 358 if (year != getYear()) { 359 this.month = 12; 360 } else { 361 this.month = getMonth(); 362 } 363 for (int i = getYear(); i > 1950; i--) { 364 if (i == year) { 365 return yearIndex; 366 } 367 yearIndex++; 368 } 369 return yearIndex; 370 } 371 372 /** 373 * 设置月份 374 * @param month 375 * @return 376 */ 377 public int setMonth(int month) { 378 int monthIndex = 0; 379 calDays(currentYear, month); 380 for (int i = 1; i < this.month; i++) { 381 if (month == i) { 382 return monthIndex; 383 } else { 384 monthIndex++; 385 } 386 } 387 return monthIndex; 388 } 389 390 /** 391 * 计算每月多少天 392 * 393 * @param month 394 * @param year 395 */ 396 public void calDays(int year, int month) { 397 boolean leayyear = false; 398 if (year % 4 == 0 && year % 100 != 0) { 399 leayyear = true; 400 } else { 401 leayyear = false; 402 } 403 for (int i = 1; i <= 12; i++) { 404 switch (month) { 405 case 1: 406 case 3: 407 case 5: 408 case 7: 409 case 8: 410 case 10: 411 case 12: 412 this.day = 31; 413 break; 414 case 2: 415 if (leayyear) { 416 this.day = 29; 417 } else { 418 this.day = 28; 419 } 420 break; 421 case 4: 422 case 6: 423 case 9: 424 case 11: 425 this.day = 30; 426 break; 427 } 428 } 429 if (year == getYear() && month == getMonth()) { 430 this.day = getDay(); 431 } 432 } 433 }
接下来,把 布局文件都展现给大家了
dialog_myinfo_changebirth
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/ly_myinfo_changebirth" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="#00000000" 7 android:gravity="center" 8 android:orientation="vertical" > 9 10 <LinearLayout 11 android:id="@+id/ly_myinfo_changebirth_child" 12 android:layout_width="300dip" 13 android:layout_height="wrap_content" 14 android:layout_marginLeft="27dip" 15 android:layout_marginRight="27dip" 16 android:background="@drawable/bg_dialog_selector" 17 android:orientation="vertical" > 18 19 <TextView 20 android:id="@+id/tv_share_title" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_marginBottom="14dip" 24 android:layout_marginLeft="18dip" 25 android:layout_marginTop="18dip" 26 android:gravity="center_vertical" 27 android:text="选择日期" 28 android:textColor="#000000" 29 android:textSize="18sp" /> 30 <LinearLayout 31 android:layout_width="match_parent" 32 android:layout_height="160dip" 33 android:orientation="horizontal" 34 android:layout_marginLeft="20dip" 35 android:layout_marginRight="20dip" 36 android:gravity="center_vertical"> 37 <com.egojit.android.pspsp.app.widget.views.WheelView 38 android:id="@+id/wv_birth_year" 39 android:layout_width="0dip" 40 android:layout_weight="1" 41 android:layout_height="match_parent" 42 android:layout_gravity="center_vertical" 43 /> 44 <com.egojit.android.pspsp.app.widget.views.WheelView 45 android:id="@+id/wv_birth_month" 46 android:layout_width="0dip" 47 android:layout_weight="1" 48 android:layout_height="match_parent" 49 android:layout_gravity="center_vertical" 50 /> 51 <com.egojit.android.pspsp.app.widget.views.WheelView 52 android:id="@+id/wv_birth_day" 53 android:layout_width="0dip" 54 android:layout_weight="1" 55 android:layout_height="match_parent" 56 android:layout_gravity="center_vertical" 57 /> 58 </LinearLayout> 59 <RelativeLayout 60 android:layout_width="match_parent" 61 android:layout_height="wrap_content" 62 android:layout_marginTop="25dip" 63 android:layout_marginBottom="21dip"> 64 65 <TextView 66 android:id="@+id/btn_myinfo_sure" 67 android:layout_width="77dip" 68 android:layout_height="33dip" 69 android:layout_alignParentRight="true" 70 android:layout_marginRight="15dip" 71 android:text="确认" 72 android:gravity="center" 73 android:textSize="16sp" 74 android:textColor="#333333"/> 75 76 77 <TextView 78 android:id="@+id/btn_myinfo_cancel" 79 android:layout_width="77dip" 80 android:layout_height="33dip" 81 android:layout_toLeftOf="@+id/btn_myinfo_sure" 82 android:text="取消" 83 android:gravity="center" 84 android:textSize="16sp" 85 android:textColor="#333333"/> 86 </RelativeLayout> 87 </LinearLayout> 88 89 </LinearLayout>
item_birth_year
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="35dip" 5 android:orientation="horizontal" 6 android:gravity="center" 7 > 8 9 <TextView 10 android:id="@+id/tempValue" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:gravity="center" 14 android:textColor="#000000" 15 /> 16 17 </LinearLayout>
R.style.ShareDialog
1 <!-- 分享对话框样式 --> 2 <style name="ShareDialog" parent="@android:Theme.Dialog"> 3 <item name="android:windowBackground">@android:color/transparent</item> 4 <item name="android:windowFrame">@null</item> 5 <item name="android:windowNoTitle">true</item> 6 <item name="android:windowIsFloating">false</item> 7 <item name="android:windowIsTranslucent">true</item> 8 <item name="android:windowContentOverlay">@null</item> 9 <item name="android:backgroundDimEnabled">true</item> 10 </style>
最后看看怎么使用的吧
1 @Event(R.id.findtime) 2 private void onFindTime(View view) { 3 MyDatePickerDialog mDialog = new MyDatePickerDialog(this, true); 4 String sTime = findTime.getText().toString(); 5 if (StringUtils.isEmpty(sTime)) { 6 Calendar mCalendar = Calendar.getInstance(); 7 y = mCalendar.get(Calendar.YEAR); 8 m = mCalendar.get(Calendar.MONTH) + 1; 9 d = mCalendar.get(Calendar.DAY_OF_MONTH); 10 } 11 mDialog.setDate(y, m, d); 12 mDialog.show(); 13 mDialog.setBirthdayListener(new MyDatePickerDialog.OnBirthListener() { 14 @Override 15 public void onClick(String year, String month, String day) { 16 //清空当前时间 17 y = Integer.parseInt(year); 18 m = Integer.parseInt(month); 19 d = Integer.parseInt(day); 20 findTime.setText(year + "-" + month + "-" + day); 21 } 22 }); 23 }
效果展示
最大时间就是当前时间