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

自定义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);

}

}

//调用方式

final calendar calendar = calendar.getinstance();

   //没有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();

 
 

解决7.0系统使用该方式调用崩溃的情况(只遇到在小米7.0系统崩溃,华为等7.0不会崩溃)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值