我想设置一个日期间隔,当用户按结束日期时,绘制一个图表。我遇到的问题是图表是在开始和结束日期绘制的。
我试图设置一个布尔变量或一个int来决定何时应该绘制图表,但是当我使用“DatePickerFragmet”中的“onDateSet”时,我无法修改其参数...我粘贴了我的代码这里是为了澄清我想解释的内容:
在我的主要活动中,我有:
startDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i=0; i<2; i++){
showDatePickerDialog(v,i);
}
}
});
public void showDatePickerDialog(View v, int i) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
然后在一个新类上我调用onDateSet:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
LineGraph lineGraph = new LineGraph();
lineGraph.run(getActivity());
}
...如何在选择最终日期时绘制图表?
答案 0 :(得分:3)
根据newFragment.show();
在这里,您必须分别为每个日期选择器编写showDatePickerDialog()
方法,以便每个日期选择器保留不同的标记。
我已经实现了如下:
public void showDeadlineDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "deadlineDate");
}
public void onDateSet(DatePicker view, int year, int month, int day) {
StringBuilder task = new StringBuilder();
task.append(day);
task.append(".");
task.append(month+1);
task.append(".");
task.append(year);
String x=task.toString();
if (getTag()=="deadlineDate")
{
btn=(Button) getActivity().findViewById(R.id.btn_deadline_date);
btn.setText(x);
}
if (getTag()=="reminderDate")
{
btn=(Button) getActivity().findViewById(R.id.btn_reminder_date);
btn.setText(x);
}
}
答案 1 :(得分:0)
由于我找不到像前面提到的那样做的方法,我通过使用2个按钮(开始和结束)来选择日期,而不是仅仅使用一个循环来达到解决方案。 之后,我创建了另一个类,每个按钮调用一个不同的类。
声明按钮和方法“showStartDatePicker”和“showEndDatePicker”:
final Button startDate = (Button)findViewById(R.id.startDate);
final Button endDate = (Button)findViewById(R.id.endDate);
public void showStartDatePicker(View v) {
DialogFragment newFragment = new StartDatePickerFragment();
newFragment.show(getFragmentManager(), "StartDatePicker");
}
public void showEndDatePicker(View v) {
DialogFragment newFragment = new EndDatePickerFragment();
newFragment.show(getFragmentManager(), "EndDatePicker");
}
类“StartDatePickerFragment”:
public class StartDatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
Toast.makeText(getActivity(), "Fecha seleccionada: " + day + "/" + (month+1) + "/" + year, Toast.LENGTH_SHORT).show();
}
}
Class“EndDatePickerFragment”:
public class EndDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
LineGraph lineGraph = new LineGraph();
lineGraph.run(getActivity());
}
}
我差不多两次做同样的事情(创建DatePicker),但我找不到更有效的方法。