在另外两个文本视图获取值之后设置文本视图值[重复]
Posted
技术标签:
【中文标题】在另外两个文本视图获取值之后设置文本视图值[重复]【英文标题】:set textview value after another two textview get value [duplicate] 【发布时间】:2020-10-07 17:02:30 【问题描述】:我创建了一个预订酒店的应用程序,它有 3 个文本视图和 2 个按钮来选择日期。如果按钮单击它会显示一个日期,并且选定的日期将显示到 2 个文本视图。如何在第三个文本视图中显示日期之间的日期? 这是我的代码(第三个文本视图不显示为日期之间的日期)
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
tvDateResult = (TextView) findViewById(R.id.tv_dateresult);
res = findViewById(R.id.tv_dateresult1);
ress = findViewById(R.id.tv_dateresult2);
btDatePicker = (Button) findViewById(R.id.bt_datepicker);
date = findViewById(R.id.bt_datepicker1);
get = findViewById(R.id.getprice);
date.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
showDateDialog2();
);
btDatePicker.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
showDateDialog();
);
private void showDateDialog2()
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
res.setText(dateFormatter.format(newDate.getTime()));
,newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
if(tvDateResult !=null || res !=null)
try
String a = tvDateResult.getText().toString();
String b = res.getText().toString();
Date aa = dateFormatter.parse(a);
Date bb = dateFormatter.parse(b);
long diff = bb.getTime() - aa.getTime() / 24 * 60 * 60 * 1000;
ress.setText((int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
catch (ParseException e)
e.printStackTrace();
private void showDateDialog()
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tvDateResult.setText(dateFormatter.format(newDate.getTime()));
,newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
【问题讨论】:
我已经发布了答案。如果您有任何问题,请发表评论。如果它有效,请接受并支持答案。 顺便考虑扔掉长期过时且臭名昭著的麻烦SimpleDateFormat
和朋友,并将ThreeTenABP添加到您的android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。
【参考方案1】:
声明2个全局变量:
Calendar date1 = null, date2 = null;
在两个日期选择器的onDateSet
方法中,将所选日期保存在全局变量中。
在第一个日期选择器的onDateSet
中,
Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
同样,在第二个日期选择器的onDateSet
中,
Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
从两个onDateSet
方法调用一个方法。
private void updateTextView()
if(date1 != null && date2 != null)
tvDateResult .setText(dateFormatter.format(daysBetween(date1,date2)));
public static long daysBetween(Calendar startDate, Calendar endDate)
// Make sure we don't change the parameter passed
Calendar newStart = Calendar.getInstance();
newStart.setTimeInMillis(startDate.getTimeInMillis());
newStart.set(Calendar.HOUR_OF_DAY, 0);
newStart.set(Calendar.MINUTE, 0);
newStart.set(Calendar.SECOND, 0);
newStart.set(Calendar.MILLISECOND, 0);
Calendar newEnd = Calendar.getInstance();
newEnd.setTimeInMillis(endDate.getTimeInMillis());
newEnd.set(Calendar.HOUR_OF_DAY, 0);
newEnd.set(Calendar.MINUTE, 0);
newEnd.set(Calendar.SECOND, 0);
newEnd.set(Calendar.MILLISECOND, 0);
long end = newEnd.getTimeInMillis();
long start = newStart.getTimeInMillis();
return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));
参考:How to get number of days between two calendar instance?
【讨论】:
以上是关于在另外两个文本视图获取值之后设置文本视图值[重复]的主要内容,如果未能解决你的问题,请参考以下文章