我已经从第一个EditText中的DatePicker输入日期,但它只获得当前日期,当我从第一个EditText中的日期选择器中选择日期时,当我在第二个编辑文本中插入任何int值时,它将更改日期在第3编辑文本。那么我怎么能做到这一点,我尝试了5个小时,但无法得到适当的解决方案。有人可以帮助我。感谢您的光临。
例如:
这是我的活动代码。
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
etReplacementDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
etReplacementDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_OF_REPLACEMENT);
}
});
String fixedDate = etReplacementDate.getText().toString().trim();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
convertedDate = new Date();
try
{
convertedDate = dateFormat.parse(fixedDate);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Date convertedDate = " + convertedDate);
String intervalDays = etInterval_Days.getText().toString().trim();
if(intervalDays.trim().length()>0){
try
{
intConvertDays =Integer.parseInt(intervalDays);
}
catch(NumberFormatException ne){
System.out.println("could not parse :: " +ne);
}
}
System.out.println("strConvertDays in afterTextChanged: " + intConvertDays);
etInterval_Days.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable editable)
{
// TODO Auto-generated method stub
String intervalDays = etInterval_Days.getText().toString().trim();
if(intervalDays.trim().length()>0){
try
{
intConvertDays =Integer.parseInt(intervalDays);
}
catch(NumberFormatException ne){
System.out.println("could not parse :: " +ne);
}
}
cal2.setTime(convertedDate);
cal2.add(Calendar.MONTH, intConvertDays);
Date resultDate = cal2.getTime();
String strResultDate = new SimpleDateFormat("yyyy-MM-dd").format(resultDate);
System.out.println("After one days strResultDate : " + strResultDate);
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");
String strConverted_Date = simpleDate.format(resultDate);
etNextReplanishmentDate.setText(strConverted_Date);
cal2.add(Calendar.DATE, -2);
Date beforDate = cal2.getTime();
String beforDate_String = new SimpleDateFormat("yyyy-MM-dd").format(beforDate);
System.out.println("beforDate_String: " + beforDate_String);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_OF_REPLACEMENT:return new DatePickerDialog(this, pickerListenerReplacement, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListenerReplacement = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
etReplacementDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
答案 0 :(得分:1)
尝试这样的事情 -
String finalDate = day + " " + month + " " + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(finalDate));
} catch (ParseException e) {
e.printStackTrace();
}
int addDays = Integer.parseInt(editText2.getText().toString());
c.add(Calendar.DATE, addDays); // number of days to add
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String output = sdf1.format(c.getTime());
要在editText3中自动更改日期,您可以使用TextWatcher -
TextWatcher tw = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
//get values in editText1 and 2 and add them
int val1 = Integer.parseInt(editText1.getText().toString());
int val2 = Integer.parseInt(editText2.getText().toString());
//add the above code here
String finalDate = day + " " + month + " " + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(finalDate));
} catch (ParseException e) {
e.printStackTrace();
}
String addDays = Integer.parseInt(editText2.getText().toString());
c.add(Calendar.DATE, addDays); // number of days to add
SimpleDateFormat sdf1 = new SimpleDateFormat("dd MMMM yyyy");
String output = sdf1.format(c.getTime());
editText3.setText(output + "");
}
};