gpt4 book ai didi

java - Android在输入其他编辑文本值时更改编辑文本值

转载 作者:行者123 更新时间:2023-11-30 02:44:32 28 4
gpt4 key购买 nike

我在第一个 EditText 中输入了 DatePicker 的日期,但它只得到当前日期,当我在第一个 EditText 中从日期选择器中选择日期时,当我在第二个 Edit 文本中插入任何 int 值时,它会改变日期在第三次编辑文本中。那么我怎么能做到这一点我已经尝试了 5 个小时但无法得到适当的解决方案。有人可以帮助我吗?感谢欣赏。

例如:

  1. 我在 = 1st Edittext 2014 年 9 月 15 日输入日期
  2. 我在 = 2nd Edit 文本中输入 5 位数字
  3. 日期应显示在 3rd Edit Text 中,例如 2014 年 9 月 20 日。

这是我的 Activity 代码。

// 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(" "));



}
};

最佳答案

尝试这样的事情-

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 + "");


}
};

关于java - Android在输入其他编辑文本值时更改编辑文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25305447/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com