我创造了AgeCalculator
我想验证DatePickerDialog ..
在我的应用程序中,当应用程序加载时系统日期将显示
之后,当我从DatePicker设置生日时,Date设置为TextView。
但是用户无法设置比当前日期更大的活动......
这是我的代码
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 :(得分:1)
将最长日期设为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;
}