自定义dialog
package com.poptest;
2.
3.import android.app.DatePickerDialog;
4.import android.content.Context;
5.import android.view.View;
6.import android.view.ViewGroup;
7.import android.widget.DatePicker;
8.//dialog类
9.
10.public class YearPickerDialog extends DatePickerDialog {
11. public YearPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
12. super(context, callBack, year, monthOfYear, dayOfMonth);
13.
14. this.setTitle(year + "年" + (monthOfYear + 1) + "月");
15. //getChildAt(2)隐藏日的显示可以改变隐藏的对象
16. ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
17. ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
18.
19. }
20.
21. public YearPickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
22. super(context, theme, listener, year, monthOfYear, dayOfMonth);
23.
24. this.setTitle(year + "年" + (monthOfYear + 1) + "月");
25. ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
26. ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
27. }
28.
29. @Override
30. public void onDateChanged(DatePicker view, int year, int month, int day) {
31. super.onDateChanged(view, year, month, day);
32. this.setTitle(year + "年" + (month + 1) + "月");
33. }
34.}
时间处理类
//时间处理类
2.package com.poptest;
3.
4.import java.text.ParseException;
5.import java.text.SimpleDateFormat;
6.import java.util.Calendar;
7.import java.util.Date;
8.
9.public class DateUtil {
10.
11. public static Date strToDate(String style, String date) {
12. SimpleDateFormat formatter = new SimpleDateFormat(style);
13. try {
14. return formatter.parse(date);
15. } catch (ParseException e) {
16. e.printStackTrace();
17. return new Date();
18. }
19. }
20.
21. public static String dateToStr(String style, Date date) {
22. SimpleDateFormat formatter = new SimpleDateFormat(style);
23. return formatter.format(date);
24. }
25.
26. public static String clanderTodatetime(Calendar calendar, String style) {
27. SimpleDateFormat formatter = new SimpleDateFormat(style);
28. return formatter.format(calendar.getTime());
29. }
30.
31. public static String DateTotime(long date, String style) {
32. SimpleDateFormat formatter = new SimpleDateFormat(style);
33. return formatter.format(date);
34. }
35.
36.}
//调用方式
2. final Calendar calendar = Calendar.getInstance();
//没有AlertDialog.THEME_HOLO_LIGHT这个Theme出来的dialog非常丑
3. new YearPickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
4. @Override
5. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
6.
7. calendar.set(Calendar.YEAR, year);
8. calendar.set(Calendar.MONTH, monthOfYear);
9. Log.d("###",DateUtil.clanderTodatetime(calendar, "yyyy-MM"));
10.
11. }
12. }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show();
解决7.0系统使用该方式调用崩溃的情况(只遇到在小米7.0系统崩溃,华为等7.0不会崩溃)
package com.dmos;
2.
3.import android.app.DatePickerDialog;
4.import android.content.Context;
5.import android.os.Bundle;
6.import android.widget.DatePicker;
7.import android.widget.LinearLayout;
8.import android.widget.NumberPicker;
9.
10.public class MyDatePickerDialog extends DatePickerDialog{
11.
12. public MyDatePickerDialog(Context context, int theme,
13. OnDateSetListener callBack, int year, int monthOfYear,
14. int dayOfMonth) {
15. super(context, theme, callBack, year, monthOfYear, dayOfMonth);
16. }
17. @Override
18. protected void onCreate(Bundle savedInstanceState) {
19. super.onCreate(savedInstanceState);
20. LinearLayout mSpinners = (LinearLayout) findViewById(getContext().getResources().getIdentifier("android:id/pickers", null, null));
21. if (mSpinners != null) {
22. NumberPicker mYearSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/year", null, null));
23. NumberPicker mMonthSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/month", null, null));
24. NumberPicker mDaySpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/day", null, null));
25. mSpinners.removeAllViews();
26. //如果要隐藏年,月,日中的某一项取消其addView就好了
27. if (mYearSpinner != null) {
28. mSpinners.addView(mYearSpinner);
29. }
30. if (mMonthSpinner!= null) {
31. mSpinners.addView(mMonthSpinner);
32. }
33. if (mDaySpinner != null) {
34. mSpinners.addView(mDaySpinner);
35. }
36. }
37. }
38. @Override
39. public void onDateChanged(DatePicker view, int year, int month, int day) {
40. super.onDateChanged(view, year, month, day);
41. setTitle(year+"年"+(month+1)+"月");
42. }
43.}