本文介绍了日期选择器对话框验证...!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个计算时代的应用程序。结果
这个程序能正常工作,但现在我希望把验证在日期选择器对话框。结果
当用户通过日期选择器对话框,搜索设置日期
然后点击用户无法设置BIRTHDATE GREATERTHAN CURRENT DATE ...
结果
我该怎么办?结果
这里是我的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;
}}
解决方案
我得到了解决。
我用低于code在我的设置验证的应用程序在 DataPicker
对话框
。
//全局声明 日历preferDate1 =新的GregorianCalendar();
日历preferDate2 =新的GregorianCalendar();
最后的日历C = Calendar.getInstance();
年= c.get(Calendar.YEAR);
月= c.get(的Calendar.MONTH);
天= c.get(Calendar.DAY_OF_MONTH);
私人诠释的年,月,日;
//这里preferredDate1和preferredDate2是的TextView
preferredDate1.setOnClickListener(新OnClickListener(){ @燮pressLint(NewApi)
@覆盖
公共无效的onClick(视图v){
DatePickerDialog DP =新DatePickerDialog(MainActivity.this,prefferedDate1Picker,preferDate1
获得(Calendar.YEAR),preferDate1.get(的Calendar.MONTH),preferDate1.get(Calendar.DAY_OF_MONTH));
。dp.getDatePicker()setMinDate(c.getTimeInMillis());
dp.show();
}
}); 私人最终DatePickerDialog.OnDateSetListener prefferedDate1Picker =新DatePickerDialog.OnDateSetListener(){ @覆盖
公共无效onDateSet(查看的DatePicker,诠释selectedYear,诠释selectedMonth,诠释selectedDay){
preferDate1 =新的GregorianCalendar(selectedYear,selectedMonth,selectedDay);
preferredDate2.setText();
preferredDate1.setText((selectedDay小于100+将String.valueOf(selectedDay):将String.valueOf(selectedDay))+/
+(selectedMonth + 1小于100+将String.valueOf((selectedMonth + 1)):将String.valueOf((selectedMonth + 1)))
+/+ selectedYear); }
};
//这将根据preferredDate1设置日期
//根据你的需要改变这里。如果你想添加或删除1个月然后根据数据集。在此我想补充一天第二个数据采集器。
preferredDate2.setOnClickListener(新OnClickListener(){ @燮pressLint(NewApi)
@覆盖
公共无效的onClick(视图v){
DatePickerDialog DP2 =新DatePickerDialog(MainActivity.this,prefferedDate2Picker,preferDate2
获得(Calendar.YEAR),preferDate2.get(的Calendar.MONTH),preferDate2.get(Calendar.DAY_OF_MONTH)); 长毫= preferDate1.getTimeInMillis()+ 86400000L;
dp2.getDatePicker()setMinDate(毫)。
dp2.show(); }
}); 私人最终DatePickerDialog.OnDateSetListener prefferedDate2Picker =新DatePickerDialog.OnDateSetListener(){ @覆盖
公共无效onDateSet(查看的DatePicker,诠释selectedYear,诠释selectedMonth,诠释selectedDay){
preferDate2 =新的GregorianCalendar(selectedYear,selectedMonth,selectedDay + 1);
preferredDate2.setText((selectedDay小于100+将String.valueOf(selectedDay):将String.valueOf(selectedDay))+/
+(selectedMonth + 1小于100+将String.valueOf((selectedMonth + 1)):将String.valueOf((selectedMonth + 1)))
+/+ selectedYear); }
};
I create app for calculate AGE.
This app works fine, but now I want to put Validation in datepicker Dialog.
when user set date through datepicker dialog,
then USER CAN NOT SET BIRTHDATE GREATERTHAN CURRENT DATE...
what should I do?
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;
}
}
解决方案
I got the solution.
I use below code in my application for set validation in DataPicker
Dialog
.
// Global declaration
Calendar preferDate1 = new GregorianCalendar();
Calendar preferDate2 = new GregorianCalendar();
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
private int year, month, day;
// here preferredDate1 and preferredDate2 are textview
preferredDate1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp = new DatePickerDialog(MainActivity.this, prefferedDate1Picker, preferDate1
.get(Calendar.YEAR), preferDate1.get(Calendar.MONTH), preferDate1.get(Calendar.DAY_OF_MONTH));
dp.getDatePicker().setMinDate(c.getTimeInMillis());
dp.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate1Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate1 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay);
preferredDate2.setText("");
preferredDate1.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
// This will set date based on preferredDate1
// change here according to your need. If you want to add or remove one month then set based on data. In this I add one day in second data picker.
preferredDate2.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp2 = new DatePickerDialog(MainActivity.this, prefferedDate2Picker, preferDate2
.get(Calendar.YEAR), preferDate2.get(Calendar.MONTH), preferDate2.get(Calendar.DAY_OF_MONTH));
long mili = preferDate1.getTimeInMillis() + 86400000L;
dp2.getDatePicker().setMinDate(mili);
dp2.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate2Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate2 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay+1);
preferredDate2.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
这篇关于日期选择器对话框验证...!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!