本文介绍了如何验证DatePickerDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建AgeCalculator。结果
我想验证DatePickerDialog ..结果
在我的应用程序时,应用程序是负载系统日期将显示结果
之后,当我从DatePicker的设置生日,日期设置为TextView的。
结果但用户无法设置BIRTHDATE BIGGER于当前日期... 结果
这里是我的code
AgeCalculator.java
包com.example.agecalculator;进口的java.util.Calendar;进口android.app.Activity;
进口android.app.DatePickerDialog;
进口android.app.Dialog;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.DatePicker;
进口android.widget.TextView;公共类AgeCalculator扩展活动实现OnClickListener {
TextView中的currentdate,出生日期,ageDisplay;
AgeCalculation年龄=无效;
静态最终诠释DATE_START_DIALOG_ID = 0;
私人INT startYear = 2015;
私人INT startMonth = 0;
私人INT朝九特派= 8; @覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
年龄=新AgeCalculation();
的currentdate =(的TextView)findViewById(R.id.txtSysDate);
currentDate.setText(age.getCurrentDate());
生日=(的TextView)findViewById(R.id.txtBirthDate);
birthDate.setOnClickListener(本);
ageDisplay =(的TextView)findViewById(R.id.txtAge);
日历有效= Calendar.getInstance();
INT validYear = valid.get(Calendar.YEAR);
INT validMonth = valid.get(的Calendar.MONTH);
INT validDay = valid.get(Calendar.DAY_OF_MONTH); } @覆盖
保护对话框onCreateDialog(INT ID){
开关(ID){
案例DATE_START_DIALOG_ID:
返回新DatePickerDialog(这一点,mDateSetListener,startYear,
startMonth,朝九特派);
}
返回null;
} 私人最终DatePickerDialog.OnDateSetListener mDateSetListener =新DatePickerDialog.OnDateSetListener(){
@覆盖
公共无效onDateSet(查看的DatePicker,诠释selectedYear,
INT selectedMonth,诠释selectedDay){
startYear = selectedYear;
startMonth = selectedMonth;
朝九特派= selectedDay;
age.setDateOfBirth(startYear,startMonth,朝九特派);
birthDate.setText(selectedDay + - +(startMonth + 1)+ -
+ startYear);
calculateAge();
}
}; @覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.txtBirthDate:
的ShowDialog(DATE_START_DIALOG_ID);
打破; 默认:
打破;
}
} 私人无效calculateAge(){
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay(); ageDisplay.setText(age.getResult());
}
// TODO自动生成方法存根}
AgeCalculation.java
包com.example.agecalculator;进口的java.util.Calendar;公共类AgeCalculation {
私人日历结束;
私人日历启动;
私人诠释endYear;
私人诠释endMonth;
私人诠释endday指定;
私人诠释startYear;
私人诠释startMonth;
私人诠释朝九特派;
私人诠释calYear;
私人诠释calMonth;
私人诠释calDay; 公共字符串getCurrentDate(){
结束= Calendar.getInstance();
endYear = end.get(Calendar.YEAR);
endMonth = end.get(的Calendar.MONTH);
endMonth ++;
endday指定= end.get(Calendar.DAY_OF_MONTH);
返回endday指定+ - + endMonth + - + endYear; } 公共无效setDateOfBirth(INT SYEAR,诠释sMonth,诠释SDAY){
startYear = SYEAR;
startMonth = sMonth;
startMonth ++;
朝九特派= SDAY; 如果(SYEAR> endYear){ } } 公共无效calcualteYear(){
calYear = endYear - startYear; } 公共无效calcualteMonth(){
如果(endMonth> = startMonth){
calMonth = endMonth - startMonth;
}其他{
calMonth = endMonth - startMonth;
calMonth = 12 + calMonth;
calYear--;
} } 公共无效calcualteDay(){ 如果(endday指定> =朝九特派){
calDay = endday指定 - 朝九特派;
}其他{
calDay = endday指定 - 朝九特派;
calDay = 30 + calDay;
如果(calMonth == 0){
calMonth = 11;
calYear--;
}其他{
calMonth--;
} }
} 公共字符串的getResult(){
回归日:+ calDay +\\ n+月+ calMonth +\\ n+年:
+ calYear;
} 众长getSeconde(){
开始= Calendar.getInstance();
start.set(Calendar.YEAR,startYear);
start.set(的Calendar.MONTH,startMonth);
start.set(Calendar.DAY_OF_MONTH,朝九特派);
start.set(Calendar.HOUR,12);
start.set(Calendar.MINUTE,30);
start.set(Calendar.SECOND,60);
start.set(Calendar.MILLISECOND,60);
长今= end.getTimeInMillis();
长老= start.getTimeInMillis();
长差异=老 - 现在;
返回差异/ 1000;
}}
解决方案
在设定最高日的DatePicker
。
@覆盖
保护对话框onCreateDialog(INT ID){
开关(ID){
案例DATE_START_DIALOG_ID:
DatePickerDialog对话框=新DatePickerDialog(这一点,mDateSetListener,
startYear,startMonth,朝九特派);
// maxDateLong可以System.currentTimeMillis的()
。dialog.getDatePicker()setMaxDate(maxDateLong);
返回对话框;
}
返回null;
}
I create AgeCalculator.
I want to validate DatePickerDialog..
In my App when app is load system date will display
after that when I set birthdate from DatePicker, Date is set to TextView.
BUT USER CAN NOT SET BIRTHDATE BIGGER THAN CURRENT DATE...
here is my code
AgeCalculator.java
package com.example.agecalculator;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
public class AgeCalculator extends Activity implements OnClickListener {
TextView currentDate, birthDate, ageDisplay;
AgeCalculation age = null;
static final int DATE_START_DIALOG_ID = 0;
private int startYear = 2015;
private int startMonth = 0;
private int startDay = 8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age = new AgeCalculation();
currentDate = (TextView) findViewById(R.id.txtSysDate);
currentDate.setText(age.getCurrentDate());
birthDate = (TextView) findViewById(R.id.txtBirthDate);
birthDate.setOnClickListener(this);
ageDisplay = (TextView) findViewById(R.id.txtAge);
Calendar valid = Calendar.getInstance();
int validYear = valid.get(Calendar.YEAR);
int validMonth = valid.get(Calendar.MONTH);
int validDay = valid.get(Calendar.DAY_OF_MONTH);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, startYear,
startMonth, startDay);
}
return null;
}
private final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
startYear = selectedYear;
startMonth = selectedMonth;
startDay = selectedDay;
age.setDateOfBirth(startYear, startMonth, startDay);
birthDate.setText(selectedDay + "-" + (startMonth + 1) + "-"
+ startYear);
calculateAge();
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtBirthDate:
showDialog(DATE_START_DIALOG_ID);
break;
default:
break;
}
}
private void calculateAge() {
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
ageDisplay.setText(age.getResult());
}
// TODO Auto-generated method stub
}
AgeCalculation.java
package com.example.agecalculator;
import java.util.Calendar;
public class AgeCalculation {
private Calendar end;
private Calendar start;
private int endYear;
private int endMonth;
private int endDay;
private int startYear;
private int startMonth;
private int startDay;
private int calYear;
private int calMonth;
private int calDay;
public String getCurrentDate() {
end = Calendar.getInstance();
endYear = end.get(Calendar.YEAR);
endMonth = end.get(Calendar.MONTH);
endMonth++;
endDay = end.get(Calendar.DAY_OF_MONTH);
return endDay + "-" + endMonth + "-" + endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay) {
startYear = sYear;
startMonth = sMonth;
startMonth++;
startDay = sDay;
if (sYear > endYear) {
}
}
public void calcualteYear() {
calYear = endYear - startYear;
}
public void calcualteMonth() {
if (endMonth >= startMonth) {
calMonth = endMonth - startMonth;
} else {
calMonth = endMonth - startMonth;
calMonth = 12 + calMonth;
calYear--;
}
}
public void calcualteDay() {
if (endDay >= startDay) {
calDay = endDay - startDay;
} else {
calDay = endDay - startDay;
calDay = 30 + calDay;
if (calMonth == 0) {
calMonth = 11;
calYear--;
} else {
calMonth--;
}
}
}
public String getResult() {
return "Day: " + calDay + "\n" + "Month:" + calMonth + "\n" + "Year:"
+ calYear;
}
public long getSeconde() {
start = Calendar.getInstance();
start.set(Calendar.YEAR, startYear);
start.set(Calendar.MONTH, startMonth);
start.set(Calendar.DAY_OF_MONTH, startDay);
start.set(Calendar.HOUR, 12);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.SECOND, 60);
start.set(Calendar.MILLISECOND, 60);
long now = end.getTimeInMillis();
long old = start.getTimeInMillis();
long diff = old - now;
return diff / 1000;
}
}
解决方案
Set max date in DatePicker
.
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener,
startYear, startMonth, startDay);
// maxDateLong can be System.currentTimeMillis()
dialog.getDatePicker().setMaxDate(maxDateLong);
return dialog;
}
return null;
}
这篇关于如何验证DatePickerDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!