Android自定义dialog可选择展示年月日时间选择栏
自定义dialog
packagecom.poptest; importandroid.app.DatePickerDialog; importandroid.content.Context; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.DatePicker; //dialog类 publicclassYearPickerDialogextendsDatePickerDialog{ publicYearPickerDialog(Contextcontext,OnDateSetListenercallBack,intyear,intmonthOfYear,intdayOfMonth){ super(context,callBack,year,monthOfYear,dayOfMonth); this.setTitle(year+"年"+(monthOfYear+1)+"月"); //getChildAt(2)隐藏日的显示可以改变隐藏的对象 ((ViewGroup)((ViewGroup)this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); ((ViewGroup)((ViewGroup)this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); } publicYearPickerDialog(Contextcontext,inttheme,OnDateSetListenerlistener,intyear,intmonthOfYear,intdayOfMonth){ super(context,theme,listener,year,monthOfYear,dayOfMonth); this.setTitle(year+"年"+(monthOfYear+1)+"月"); ((ViewGroup)((ViewGroup)this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); ((ViewGroup)((ViewGroup)this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); } @Override publicvoidonDateChanged(DatePickerview,intyear,intmonth,intday){ super.onDateChanged(view,year,month,day); this.setTitle(year+"年"+(month+1)+"月"); } }
时间处理类
//时间处理类 packagecom.poptest; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; publicclassDateUtil{ publicstaticDatestrToDate(Stringstyle,Stringdate){ SimpleDateFormatformatter=newSimpleDateFormat(style); try{ returnformatter.parse(date); }catch(ParseExceptione){ e.printStackTrace(); returnnewDate(); } } publicstaticStringdateToStr(Stringstyle,Datedate){ SimpleDateFormatformatter=newSimpleDateFormat(style); returnformatter.format(date); } publicstaticStringclanderTodatetime(Calendarcalendar,Stringstyle){ SimpleDateFormatformatter=newSimpleDateFormat(style); returnformatter.format(calendar.getTime()); } publicstaticStringDateTotime(longdate,Stringstyle){ SimpleDateFormatformatter=newSimpleDateFormat(style); returnformatter.format(date); } }//调用方式 finalCalendarcalendar=Calendar.getInstance();
解决7.0系统使用该方式调用崩溃的情况(只遇到在小米7.0系统崩溃,华为等7.0不会崩溃)
packagecom.dmos; importandroid.app.DatePickerDialog; importandroid.content.Context; importandroid.os.Bundle; importandroid.widget.DatePicker; importandroid.widget.LinearLayout; importandroid.widget.NumberPicker; publicclassMyDatePickerDialogextendsDatePickerDialog{ publicMyDatePickerDialog(Contextcontext,inttheme, OnDateSetListenercallBack,intyear,intmonthOfYear, intdayOfMonth){ super(context,theme,callBack,year,monthOfYear,dayOfMonth); } @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); LinearLayoutmSpinners=(LinearLayout)findViewById(getContext().getResources().getIdentifier("android:id/pickers",null,null)); if(mSpinners!=null){ NumberPickermYearSpinner=(NumberPicker)findViewById(getContext().getResources().getIdentifier("android:id/year",null,null)); NumberPickermMonthSpinner=(NumberPicker)findViewById(getContext().getResources().getIdentifier("android:id/month",null,null)); NumberPickermDaySpinner=(NumberPicker)findViewById(getContext().getResources().getIdentifier("android:id/day",null,null)); mSpinners.removeAllViews(); //如果要隐藏年,月,日中的某一项取消其addView就好了 if(mYearSpinner!=null){ mSpinners.addView(mYearSpinner); } if(mMonthSpinner!=null){ mSpinners.addView(mMonthSpinner); } if(mDaySpinner!=null){ mSpinners.addView(mDaySpinner); } } } @Override publicvoidonDateChanged(DatePickerview,intyear,intmonth,intday){ super.onDateChanged(view,year,month,day); setTitle(year+"年"+(month+1)+"月"); } }
以上所述是小编给大家介绍的Android自定义dialog可选择展示年月日时间选择栏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!