片段沟通的困惑。片段内的Android DatePickerFragment / TimePickerFragment,

时间:2014-05-04 14:12:57

标签: android android-fragments

我对片段通信有点困惑。 Android说两个片段永远不应该直接通信,我不知道我的片段是否违反了这条规则。

基本上,我想要一个包含下拉微调器的Fragment,一个DatePickerDialog(一旦用户点击编辑文本就会弹出),以及一个TimePickerDialog(一旦用户点击编辑文本,它就会弹出)。

这是我到目前为止所做的,现在,当我点击编辑文本时,DatePickerDialog和TimePickerDialog将会出现,而且一切似乎都在起作用。

但我有点担心我试图让两个片段进行通信(在我的CommonSelectionFragment和DatePickerFragment之间)。我是否违反了碎片通信的规则?如果我这样做,你能告诉我什么是更好的方法来解决这个问题。非常感谢你提前。

    public class CommonSelectionFragment extends Fragment implements
    OnItemSelectedListener {

private Spinner spinner;
private EditText edtTime;
private EditText edtDate;
private static final Calendar c = Calendar.getInstance();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_common_selection,
            container, false);

    // populating the spinner with string array of time category
    spinner = (Spinner) rootView.findViewById(R.id.spinner_time_category);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity().getApplicationContext(), R.array.category_array,
            R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    edtTime = (EditText) rootView.findViewById(R.id.edt_time);
    edtTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showTimePickerDialog(v);

        }
    });

    edtDate = (EditText) rootView.findViewById(R.id.edt_enter_date);
    edtDate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            showDatePickerDialog(v);

        }
    });

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    TextView myText = (TextView) view;
    Toast.makeText(getActivity().getApplicationContext(),
            "You Selected " + myText.getText(), Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    int year, month, day;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
    //  final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        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) {
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
    }

}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getActivity().getSupportFragmentManager(),
            "datePicker");
}

public static class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        //final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getActivity().getSupportFragmentManager(),
            "timePicker");
}

}

0 个答案:

没有答案