如何在Android中的自定义AlertDialog活动中打开Edittext上的DatePicker?

时间:2018-03-31 19:22:55

标签: android datepicker android-alertdialog customdialog datepickerdialog

我需要从自定义创建的AlertDialog活动中打开DatePicker,以便设置' From Date'和'至今'。在onClick放置在AlertDialog活动中的Edittext时,将打开DatePicker。但是我试过我无法打开DatePicker。我将附上我已经完成的代码。

RequestActivity.class

const_leave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                LayoutInflater factory = LayoutInflater.from(RequestActivity.this);
                final View deleteDialogView = factory.inflate(R.layout.activity_leave_request, null);
                final AlertDialog deleteDialog = new AlertDialog.Builder(RequestActivity.this).create();

                deleteDialog.setView(deleteDialogView);
                deleteDialogView.findViewById(R.id.btn_LeaveOk).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteDialog.dismiss();
                    }
                });
                deleteDialogView.findViewById(R.id.btn_LeaveCancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        deleteDialog.dismiss();
                    }
                });
                deleteDialog.show();
        }
    });

AlertDialog活动的代码,即 LeaveRequestActivity.class

public class LeaveRequestActivity extends AppCompatActivity implements View.OnClickListener {

EditText etFromDateValue, etToDate, etRemarkValue;
TextView tvtolabel, tvdaysvalue, tvdayslabel;
Button btn_LeaveCancel, btn_LeaveOk;
Spinner spin;

private DatePickerDialogFragment mDatePickerDialogFragment;

Calendar myCalendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leave_request);

    cast();

    myCalendar = Calendar.getInstance();

    etFromDateValue.setOnClickListener(this);
    etToDate.setOnClickListener(this);

}

public void cast(){
    etFromDateValue= findViewById(R.id.etFromDateValue);
    etToDate= findViewById(R.id.etToDate);
    etRemarkValue= findViewById(R.id.etRemarkValue);
    tvtolabel = findViewById(R.id.tvtolabel);
    tvdaysvalue= findViewById(R.id.tvdaysvalue);
    tvdayslabel= findViewById(R.id.tvdayslabel);
    btn_LeaveCancel= findViewById(R.id.btn_LeaveCancel);
    btn_LeaveOk = findViewById(R.id.btn_LeaveOk);
    spin = findViewById(R.id.sp_leaveRequest);
    mDatePickerDialogFragment = new DatePickerDialogFragment();
}

@Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.etFromDateValue) {
        mDatePickerDialogFragment.setFlag(DatePickerDialogFragment.FLAG_START_DATE);
        mDatePickerDialogFragment.show(getSupportFragmentManager(), "datePicker");
    } else if (id == R.id.etToDate) {
        mDatePickerDialogFragment.setFlag(DatePickerDialogFragment.FLAG_END_DATE);
        mDatePickerDialogFragment.show(getSupportFragmentManager(), "datePicker");
    }
}

@SuppressLint("ValidFragment")
public class DatePickerDialogFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    public static final int FLAG_START_DATE = 0;
    public static final int FLAG_END_DATE = 1;

    private int flag = 0;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

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

    public void setFlag(int i) {
        flag = i;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear, dayOfMonth);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (flag == FLAG_START_DATE) {
            etFromDateValue.setText(format.format(calendar.getTime()));
        } else if (flag == FLAG_END_DATE) {
            etToDate.setText(format.format(calendar.getTime()));
        }
    }
 }

}

有没有人知道如何在AlertDialog活动上实现Datepicker请分享您的想法。提前谢谢......

0 个答案:

没有答案