欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-15 22:52:40
自定义dialog package com.poptest; import android.app.datepickerdialog; import an...

自定义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可选择展示年月日时间选择栏,希望对大家有所帮助

上一篇:

下一篇: