Android自定義dialog可選擇展示年月日時(shí)間選擇欄
自定義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)隱藏日的顯示可以改變隱藏的對(duì)象 ((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) + "月"); } }
時(shí)間處理類
//時(shí)間處理類 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">//調(diào)用方式 final Calendar calendar = Calendar.getInstance(); </pre><pre name="code" class="java"><span style="white-space:pre"> </span>//沒有AlertDialog.THEME_HOLO_LIGHT這個(gè)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> <h3><a name="t2"></a><em>解決7.0系統(tǒng)使用該方式調(diào)用崩潰的情況(只遇到在小米7.0系統(tǒng)崩潰,華為等7.0不會(huì)崩潰)</em></h3>
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(); //如果要隱藏年,月,日中的某一項(xiàng)取消其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可選擇展示年月日時(shí)間選擇欄,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android Flutter實(shí)現(xiàn)興趣標(biāo)簽選擇功能
我們?cè)谑状问褂脙?nèi)容類 App 的時(shí)候,不少都會(huì)讓我們選擇個(gè)人偏好,通過這些標(biāo)簽選擇可以預(yù)先知道用戶的偏好信息。我們本篇就來看看 Flutter 如何實(shí)現(xiàn)興趣標(biāo)簽的選擇,需要的可以參考一下2022-11-11Android使用Photoview實(shí)現(xiàn)圖片左右滑動(dòng)及縮放功能
這篇文章主要為大家詳細(xì)介紹了Android使用Photoview實(shí)現(xiàn)圖片左右滑動(dòng)及縮放功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android 通過自定義view實(shí)現(xiàn)水波紋效果案例詳解
這篇文章主要介紹了Android 通過自定義view實(shí)現(xiàn)水波紋效果案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android 軟鍵盤彈出隱藏?cái)D壓界面等各種問題小結(jié)
這篇文章主要介紹了Android 軟鍵盤彈出隱藏?cái)D壓界面等各種問題的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-11-11Android使用WebView實(shí)現(xiàn)截圖分享功能
這篇文章主要為大家詳細(xì)介紹了Android使用WebView實(shí)現(xiàn)截圖分享功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法
今天小編就為大家分享一篇關(guān)于取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Android 實(shí)現(xiàn)電話攔截及攔截提示音功能的開發(fā)
本文主要介紹Android 實(shí)現(xiàn)電話攔截和攔截提示音功能的開發(fā),這里提供實(shí)現(xiàn)代碼和詳細(xì)講解,有需要的小伙伴可以參考下2016-08-08