我创建了计算AGE的应用程序
这个应用程序工作正常,但现在我想将验证放在datepicker对话框中
当用户通过datepicker对话框设置日期时,
然后用户无法在当前大日期间设置生日...
我该怎么办?
这是我的代码...
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;
}
}
答案 0 :(得分:0)
您可以做的是禁用该项目的选择。
创建自己的ArrayAdapter子类,在其中覆盖areAllItemsEnabled()以返回false,并且isEnabled()根据需要返回true或false。
答案 1 :(得分:0)
我得到了解决方案。
我在我的应用程序中使用以下代码在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);
}
};