您好,登錄后才能下訂單哦!
自定義dialog
package com.poptest; import android.app.DatePickerDialog; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.DatePicker; //dialog類 public class YearPickerDialog extends DatePickerDialog { public YearPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { 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); } public YearPickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { 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 public void onDateChanged(DatePicker view, int year, int month, int day) { super.onDateChanged(view, year, month, day); this.setTitle(year + "年" + (month + 1) + "月"); } }
時間處理類
//時間處理類 package com.poptest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { public static Date strToDate(String style, String date) { SimpleDateFormat formatter = new SimpleDateFormat(style); try { return formatter.parse(date); } catch (ParseException e) { e.printStackTrace(); return new Date(); } } public static String dateToStr(String style, Date date) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(date); } public static String clanderTodatetime(Calendar calendar, String style) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(calendar.getTime()); } public static String DateTotime(long date, String style) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(date); } }<pre name="code" class="java">//調用方式 final Calendar calendar = Calendar.getInstance(); </pre><pre name="code" class="java"><span > </span>//沒有AlertDialog.THEME_HOLO_LIGHT這個Theme出來的dialog非常丑 new YearPickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, monthOfYear); Log.d("###",DateUtil.clanderTodatetime(calendar, "yyyy-MM")); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show(); </pre><br> <pre></pre> <h4><a name="t2"></a><em>解決7.0系統使用該方式調用崩潰的情況(只遇到在小米7.0系統崩潰,華為等7.0不會崩潰)</em></h4>
package com.dmos; import android.app.DatePickerDialog; import android.content.Context; import android.os.Bundle; import android.widget.DatePicker; import android.widget.LinearLayout; import android.widget.NumberPicker; public class MyDatePickerDialog extends DatePickerDialog{ public MyDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, theme, callBack, year, monthOfYear, dayOfMonth); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout mSpinners = (LinearLayout) findViewById(getContext().getResources().getIdentifier("android:id/pickers", null, null)); if (mSpinners != null) { NumberPicker mYearSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/year", null, null)); NumberPicker mMonthSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/month", null, null)); NumberPicker mDaySpinner = (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 public void onDateChanged(DatePicker view, int year, int month, int day) { super.onDateChanged(view, year, month, day); setTitle(year+"年"+(month+1)+"月"); } }
以上所述是小編給大家介紹的Android自定義dialog可選擇展示年月日時間選擇欄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。