多个日期监听器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多个日期监听器相关的知识,希望对你有一定的参考价值。
我有这样的代码
private void Picker() {
Calendar now = Calendar.getInstance();
Calendar maxDate = Calendar.getInstance();
maxDate.add(Calendar.MONTH, 1);
Calendar minDate = Calendar.getInstance();
minDate.add(Calendar.DAY_OF_YEAR, 1);
dataController = DatePickerDialog.newInstance(
controller.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dataController.setMinDate(minDate);
dataController.setMaxDate(maxDate);
dataController.setTitle("Data awizacji");
inputdataController.setFocusable(false);
inputdataController.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datadataController.show(getFragmentManager(), "Data");
}
});
Calendar from = Calendar.getInstance();
Calendar frommaxDate = Calendar.getInstance();
frommaxDate.add(Calendar.MONTH, 1);
Calendar fromminDate = Calendar.getInstance();
fromminDate.add(Calendar.DAY_OF_YEAR, 1);
dataControllerSecond = DatePickerDialog.newInstance(
controller.this,
from.get(Calendar.YEAR),
from.get(Calendar.MONTH),
from.get(Calendar.DAY_OF_MONTH)
);
dataControllerSecond.setMinDate(fromminDate);
dataControllerSecond.setMaxDate(frommaxDate);
dataControllerSecond.setTitle("Data awizacji DO");
inputdataControllerSecond.setFocusable(false);
inputdataControllerSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataControllerSecond.show(getFragmentManager(), "Data spotkania");
}
});
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
now = Calendar.getInstance();
now.set(year, monthOfYear, dayOfMonth);
inputdataController.setText(dateFormatter.format(now.getTime()));
from = Calendar.getInstance();
from.set(year, monthOfYear, dayOfMonth);
inputdataControllerSecond.setText(dateFormatter.format(from.getTime()));
}
我真的不知道如何让两个输入相互独立...我的意思是当我从一个输入中选择一个选项时,它会在所有输入中选择...我想做两个独立的日期...你知道我错了什么吗?我看到一个问题,答案是说另一个听众,但我做了,但它仍然无法正常工作..我很困惑,我不知道如何制作......这对我来说太烦人了...可以有人救命? :)(我不需要完美的代码,我只需要一些线索,我做的很糟糕..)
答案
在类级别声明一个变量:
int type = 1;
并改变你的听众:
inputdataController.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
type = 1;
datadataController.show(getFragmentManager(), "Data");
}
});
和
inputdataControllerSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
type = 2;
dataControllerSecond.show(getFragmentManager(), "Data spotkania");
}
});
然后在onDateSet()
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
if (type == 1) {
now = Calendar.getInstance();
now.set(year, monthOfYear, dayOfMonth);
inputdataController.setText(dateFormatter.format(now.getTime()));
} else if (type == 2) {
from = Calendar.getInstance();
from.set(year, monthOfYear, dayOfMonth);
inputdataControllerSecond.setText(dateFormatter.format(from.getTime()));
}
}
以上是关于多个日期监听器的主要内容,如果未能解决你的问题,请参考以下文章