当用户点击取消按钮时如何解除datepicker?

时间:2013-12-30 09:39:18

标签: android datepicker datetimepicker timepicker android-dialogfragment

取消按钮在我的应用程序中无效。当我单击取消按钮时,它会显示时间选择器对话框而不是日期对话框。

DatePickerFragment.java
@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    Dialog dialog;
     private boolean mIgnoreTimeSet = false;
    public EditText activity_edittext;
    //OnDismissListener mOnDismissListener;
    public DatePickerFragment(EditText edit_text) {
        activity_edittext = edit_text;
    }

    @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);

        return new DatePickerDialog(getActivity(),this, year, month, day);

    }
    @Override
    public void onDateSet(DatePicker view, int year, int month,
            int day) {
        Log.d("date",String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        activity_edittext.setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
    }

  public void onDateCancel(DatePicker view, int year, int month,int day) { 
       dialog.dismiss();        
}
}

TimePickerFragment.java
@SuppressLint("ValidFragment")
public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    int  mhour,mminute;  

    public EditText activity_edittext;

    public TimePickerFragment(EditText edit_text) {
        activity_edittext = edit_text;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();

        mhour  = c.get(Calendar.HOUR_OF_DAY);
        mminute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(),this, mhour,  mminute,false);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        mhour = hourOfDay;
        mminute = minute;
        Log.d("time",String.valueOf(mhour) + ":"
                + String.valueOf(mminute));
        activity_edittext.setText(String.valueOf(mhour) + ":"
                + String.valueOf(mminute)) ;
    }
}


TimeFragment.java
public  class TimeFragment extends Fragment{    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showTimePickerDialog(new EditText(getActivity()));
    }

    public void showDatePickerDialog(View v) {
        DatePickerFragment newFragment = new DatePickerFragment(new EditText(getActivity()));
        newFragment.show(getFragmentManager(), "datePicker");
        NowFragment fragment = new NowFragment();

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();
        }
    }

    private void showTimePickerDialog(EditText editText) {
         TimePickerFragment newFragment = new TimePickerFragment(new EditText(getActivity()));
            newFragment.show(getFragmentManager(), "timePicker");
            showDatePickerDialog(new EditText(getActivity()));
    }
}

3 个答案:

答案 0 :(得分:1)

看起来Jellybean存在一个错误,其中“取消”按钮不起作用(因此后退按钮)。这在Jelly Bean DatePickerDialog — is there a way to cancel?中进行了讨论。

David Cesarino,在上述帖子中报告了错误和解决方法,发布了他的解决方案here Cavega略微修改了上述解决方案,以允许将DatePickerDialog中的日期初始化为今天的date.Code以外的其他内容here。我使用了他的解决方案并让它发挥作用。

答案 1 :(得分:-1)

请删除

setCancelable(false);

并设置此行而不是上一行:

setCanceledOnTouchOutside(false);

答案 2 :(得分:-1)

试试这个

  

getDialog()驳回();

同时删除

  

setCancelable(假);