Android自定义dialog可选择展示年月日时间选择栏

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

自定义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 style="white-space:pre">   </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> 
<h3><a name="t2"></a><em>解决7.0系统使用该方式调用崩溃的情况(只遇到在小米7.0系统崩溃,华为等7.0不会崩溃)</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();  
     //如果要隐藏年,月,日中的某一项取消其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可选择展示年月日时间选择栏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多