SoFunction
Updated on 2025-03-11

Android custom dialog can select to display year, month, day and time selection bar

Custom dialog

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
//dialog classpublic class YearPickerDialog extends DatePickerDialog { 
  public YearPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { 
    super(context, callBack, year, monthOfYear, dayOfMonth);  
    (year + "Year" + (monthOfYear + 1) + "moon"); 
    //getChildAt(2) The display of hidden day can change the hidden object   ((ViewGroup) ((ViewGroup) ().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(); 
   ((ViewGroup) ((ViewGroup) ().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(); 
  }  
  public YearPickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { 
    super(context, theme, listener, year, monthOfYear, dayOfMonth); 
 
   (year + "Year" + (monthOfYear + 1) + "moon"); 
    ((ViewGroup) ((ViewGroup) ().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(); 
    ((ViewGroup) ((ViewGroup) ().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(); 
  }  
  @Override 
 public void onDateChanged(DatePicker view, int year, int month, int day) { 
    (view, year, month, day); 
    (year + "Year" + (month + 1) + "moon"); 
  } 
} 

Time processing class

//Time Processing Classpackage ;  
import ; 
import ; 
import ; 
import ;  
public class DateUtil { 
  public static Date strToDate(String style, String date) { 
    SimpleDateFormat formatter = new SimpleDateFormat(style); 
    try { 
      return (date); 
    } catch (ParseException e) { 
      (); 
      return new Date(); 
    } 
  }  
  public static String dateToStr(String style, Date date) { 
   SimpleDateFormat formatter = new SimpleDateFormat(style); 
    return (date); 
  }  
  public static String clanderTodatetime(Calendar calendar, String style) { 
    SimpleDateFormat formatter = new SimpleDateFormat(style); 
    return (()); 
 }  
  public static String DateTotime(long date, String style) { 
    SimpleDateFormat formatter = new SimpleDateFormat(style); 
    return (date); 
  } 
}<pre name="code" class="java">//Call method        final Calendar calendar = (); </pre><pre name="code" class="java"><span style="white-space:pre">   </span>//The dialog without AlertDialog.THEME_HOLO_LIGHT The Theme is very ugly    new YearPickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new () { 
       @Override 
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
 
        (, year); 
         (, monthOfYear); 
        ("###",(calendar, "yyyy-MM")); 
 
       } 
    }, (), (), ()).show(); 
</pre><br> 
<pre></pre> 
<h3><a name="t2"></a><em>solve7.0The system crashes when calling using this method(Only met in Xiaomi7.0System crash,Huawei etc.7.0Will not crash)</em></h3> 
package ;  
import ; 
import ;  
import ; 
import ; 
import ; 
import ;  
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) {  
  (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));  
     ();  
     //It would be great if you want to hide an item in the year, month, or day to cancel its addView     if (mYearSpinner != null) {  
      (mYearSpinner);  
    }  
     if (mMonthSpinner!= null) {  
      (mMonthSpinner);   
    }  
    if (mDaySpinner != null) {  
       (mDaySpinner);    
    }  
   }  
 } 
 @Override  
 public void onDateChanged(DatePicker view, int year, int month, int day) {  
   (view, year, month, day);  
  setTitle(year+"Year"+(month+1)+"moon");  
 } 
}  

The above is the Android custom dialog that the editor introduces to you. You can choose to display the year, month, day and time selection column. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!