1

我想设置一个日期间隔,当用户按下结束日期时,绘制一个图表。我遇到的问题是图表是在开始日期和结束日期绘制的。

我试图设置一个布尔变量或一个整数来决定何时绘制图表,但由于我使用“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());

}

... 选择最终日期后如何绘制图表?

4

2 回答 2

3

根据传入的 Tag 编写 if..else 或 switch 语句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);

     }
}
于 2012-07-27T10:51:48.750 回答
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();
}
}

“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),但我找不到更有效的方法。

于 2012-07-26T12:12:34.637 回答