DialogFragment没有显示

时间:2018-04-10 07:00:14

标签: android datepicker fragment

我想在我的Fragment Home中显示我的DialogFragment,我已经制作了Dialog Fragment并制作了类SelectDateFragment,但是当我运行我的应用程序时,没有任何错误发生,没有错误 我的Onclick文本视图显示对话框片段

void onClickDate(View view){
    txtDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                DialogFragment newFragment= new SelectDateFragment();
                newFragment.show(getFragmentManager(),"DatePicker");
            }
            catch (Exception e){
                e.toString();
            }
        }
    });
}

mySelect DateFragment

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    @RequiresApi(api = Build.VERSION_CODES.N)
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker datePicker, int yy, int mm, int dd) {
        populateSetDate(yy,mm+1,dd);
    }
    public void populateSetDate(int year, int month, int day) {
        txtDate.setText(month+"/"+day+"/"+year);
    }
}

2 个答案:

答案 0 :(得分:1)

使用此

 private void openDatePicker() {
    Calendar calendar = Calendar.getInstance();
    DatePickerDialog d = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {

            month = month + 1;
            String date = month + "/" + day + "/" + year;
            editDob.setText(date);
        }
    }, calendar
            .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH));
    d.getDatePicker().setMinDate(System.currentTimeMillis());
    d.show();
}

答案 1 :(得分:1)

使用以下方法选择日期之前和之后的用户当前日期是禁用..

 /**
 * this method used show date picker.
 * and user select current date after.
 * before data are disable.
 */
private void selectDate() {
    Calendar calendar = Calendar.getInstance();
    DatePickerDialog d = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {

            month = month + 1;
            String date = month + "/" + day + "/" + year;
            Log.d("Date::",date);
        }
    }, calendar
            .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH));
    d.getDatePicker().setMinDate(System.currentTimeMillis()-1000);
    d.show();
}