使用FragmentDialog创建一个TimePickerDialog?

时间:2014-08-13 13:16:49

标签: java android android-fragments time datepicker

我想使用FragmentDialog创建一个TimePickerDialog,并在我的片段和TimePickerDialog之间交换数据。我已经创建了一个DatePickerDialog,但我不知道如何创建一个TimePickerDialog。我希望当用户单击我的片段TimePickerDialog中的按钮时出现。 这就是我创建 DatePickerDialog

的方法
  

import java.util.Calendar; import java.util.Date;进口   java.util.GregorianCalendar中;

     

import android.app.Activity; import android.app.AlertDialog;进口   android.app.Dialog; import android.content.DialogInterface;进口   android.content.Intent; import android.os.Bundle;进口   android.support.v4.app.DialogFragment; import android.view.View;   import android.widget.DatePicker;进口   android.widget.DatePicker.OnDateChangedListener;

     

public class DatePickerFragment extends DialogFragment {       public static final String EXTRA_DATE =" criminalintent.DATE&#34 ;;

Date mDate;

public static DatePickerFragment newInstance(Date date) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_DATE, date);

    DatePickerFragment fragment = new DatePickerFragment();
    fragment.setArguments(args);

    return fragment;
}

private void sendResult(int resultCode) {
    if (getTargetFragment() == null) 
        return;

    Intent i = new Intent();
    i.putExtra(EXTRA_DATE, mDate);

    getTargetFragment()
        .onActivityResult(getTargetRequestCode(), resultCode, i);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mDate = (Date)getArguments().getSerializable(EXTRA_DATE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(mDate);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    View v = getActivity().getLayoutInflater()
        .inflate(R.layout.dialog_date, null);

    DatePicker datePicker = (DatePicker)v.findViewById(R.id.dialog_date_datePicker);
    datePicker.init(year, month, day, new OnDateChangedListener() {
        public void onDateChanged(DatePicker view, int year, int month, int day) {
            mDate = new GregorianCalendar(year, month, day).getTime();

            // update argument to preserve selected value on rotation
            getArguments().putSerializable(EXTRA_DATE, mDate);
        }
    });

    return new AlertDialog.Builder(getActivity())
        .setView(v)
        .setTitle(R.string.date_picker_title)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                sendResult(Activity.RESULT_OK);
            }
        })
        .create();
} }

CrimeFragment

  

import java.util.Date; import java.util.UUID;

     

import android.app.Activity; import android.content.Intent;进口   android.os.Bundle; import android.support.v4.app.Fragment;进口   android.support.v4.app.FragmentManager; import android.text.Editable;   import android.text.TextWatcher; import android.view.LayoutInflater;   import android.view.View; import android.view.ViewGroup;进口   android.webkit.WebView.FindListener; import android.widget.Button;   import android.widget.CheckBox; import android.widget.CompoundButton;   import android.widget.CompoundButton.OnCheckedChangeListener;进口   android.widget.EditText;

     

公共类CrimeFragment扩展Fragment {       public static final String EXTRA_CRIME_ID =" criminalintent.CRIME_ID";

private static final String DIALOG_DATE = "date";
private static final int REQUEST_DATE = 0;

Crime mCrime;
EditText mTitleField;
Button mDateButton;
CheckBox mSolvedCheckBox;
Button mTime;
String time;
public static CrimeFragment newInstance(UUID crimeId) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment();
    fragment.setArguments(args);

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}

public void updateDate() {
    mDateButton.setText(mCrime.getDate().toString());
}
  public void updateTime() {

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime, parent, false);

    mTitleField = (EditText)v.findViewById(R.id.crime_title);
    mTitleField.setText(mCrime.getTitle());
    mTitleField.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence c, int start, int before, int count) {
            mCrime.setTitle(c.toString());
        }

        public void beforeTextChanged(CharSequence c, int start, int count, int after) {
            // this space intentionally left blank
        }

        public void afterTextChanged(Editable c) {
            // this one too
        }
    });

    mDateButton = (Button)v.findViewById(R.id.crime_date);
    updateDate();
    mDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {                                 
            FragmentManager fm = getActivity()
                    .getSupportFragmentManager();
            DatePickerFragment dialog = DatePickerFragment
                .newInstance(mCrime.getDate());
            dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
            dialog.show(fm, DIALOG_DATE);                 
        }
    });
    mTime = (Button) v.findViewById(R.id.crime_time);

    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set the crime's solved property
            mCrime.setSolved(isChecked);
        }
    });



    return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_DATE) {
        Date date = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
     

mCrime.setDate(日期);                updateDate();

    }
} }

现在我要创建一个TimePickerDialog。

1 个答案:

答案 0 :(得分:0)

public class DialogFragmentTimePicker extends DialogFragment implements OnTimeSetListener {

public static final String ARG_HOUR = "hour";

public static final String ARG_MINUTE = "minute";

private OnTimeSetListener mListener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    int hour, minute;
    if (getArguments() != null) {
        hour = getArguments().getInt(ARG_HOUR);
        minute = getArguments().getInt(ARG_MINUTE);
    } else {
        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        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 setOnTimeSetListener(OnTimeSetListener listener) {

    mListener = listener;
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    if (mListener != null) {
        mListener.onTimeSet(view, hourOfDay, minute);
    }
}

}