在日期选择器中设置日期

时间:2013-12-05 10:46:10

标签: android date android-datepicker

我的按钮(5-12-2031)中有一个日期,当我点击按钮时,我想显示日期选择器的日期我的按钮。在Stackoverflow i found to set the time picker Android: Setting time in time picker with the time shown in text view中,我尝试了相同但日期选择器未显示。

我的代码是:

Calendar mDateCalender = Calendar.getInstance();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_details_main_layout);


    date.setOnClickListener(new onClickListener) {
    public void onClick(){

    new DatePickerDialog(AddDetailsActivity.this, onDateListener,
                        mDateCalender.get(Calendar.YEAR), mDateCalender.get(Calendar.MONTH),
                        mDateCalender.get(Calendar.DAY_OF_MONTH)).show();
    }
    }
}


 DatePickerDialog.OnDateSetListener onDateListener = new OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

        String dateFormat = mOfflineDatas.get(0).getOnsket_utfort();
        String[] getDates = dateFormat.split("-");

        Toast.makeText(getApplicationContext(), "" + dateFormat, Toast.LENGTH_LONG).show();

        mDateCalender.set(Calendar.DAY_OF_MONTH, Integer.valueOf(getDates[0]));
        mDateCalender.set(Calendar.MONTH, Integer.valueOf(getDates[1]));
        mDateCalender.set(Calendar.YEAR, Integer.valueOf(getDates[2]));

    }
};

我的dateFromat = 5-12-2013它正确分割。

我需要改变什么改变,任何人都提出了一些想法..

4 个答案:

答案 0 :(得分:2)

这可能对你有帮助......

yourButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        showDatePickerDialog(yourButton.getText().toString());
    }
});

private void showDatePickerDialog(String date) {
    // here date is 5-12-2013
    String[] split = date.split("-");
    int day = Integer.valueOf(split[0]);
    int month = Integer.valueOf(split[1]);
    int year = Integer.valueOf(split[2]);
    OnDateSetListener dateSetListener = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub

        }
    };

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            dateSetListener, year, month, day);
    datePickerDialog.show();
}

答案 1 :(得分:1)

试试这个..

全球:

static final int DATE_DIALOG_ID = 999;

private int myear;
private int mmonth;
private int mday;

OnCreate:

    final Calendar c = Calendar.getInstance();
    myear = c.get(Calendar.YEAR);
    mmonth = c.get(Calendar.MONTH);
    mday = c.get(Calendar.DAY_OF_MONTH);

       btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            String[] str = btn.getText().toString().trim().split("-");

            mday = Integer.parseInt(str[0]);
            mmonth = Integer.parseInt(str[1]);
            myear = Integer.parseInt(str[2]);

                showDialog(DATE_DIALOG_ID);

            }

        });

OnCreate()之外的方法:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, myear,mmonth,
                    mday){

            };
            return _date;
        }
        return null;
    }

编辑1:

date.setOnClickListener(new onClickListener) {
public void onClick(){


            String[] str = date.getText().toString().trim().split("-");

            mday = Integer.parseInt(str[0]);
            mmonth = Integer.parseInt(str[1]);
            myear = Integer.parseInt(str[2]);
new DatePickerDialog(AddDetailsActivity.this, onDateListener,
                    myear, mmonth,
                    mday).show();
}
}

答案 2 :(得分:0)

你可以在DatePicker.init设置它......

或者设置日期控件的日期,如android developer sample所示。见步骤4-6。

Here's another sample blog post on this.

或者您可以在按钮点击事件

上使用以下内容
Calendar cal=Calendar.getInstance(Locale.ENGLISH);
dp.init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);

答案 3 :(得分:0)

如果您使用片段此代码可能会对您有所帮助。

mLesson_Date = (TextView) rootView.findViewById(R.id.Lesson_Date);

--------------------------------------------------------------------
mLesson_DateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment1 = new DatePickerFragment(
                        mLesson_Date);// Here you are passing the TextView Object to DatePickerFragment by calling the constructor 
                newFragment1.show(getFragmentManager(), "datePicker");
            }
        });
--------------------------------------------------------------------

public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;
        TextView sample;

        public DatePickerFragment() {
            // TODO Auto-generated constructor stub.
        }

        public DatePickerFragment(TextView setTimeView) {
            // TODO Auto-generated constructor stub.
            this.sample = setTimeView;
        }

        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(),
            // (EditSessionActivity)getActivity(), year, month, day);

            // 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) {

            sample.setText(String.valueOf(day) + "/"
                    + String.valueOf(month + 1) + "/" + String.valueOf(year));
            // set selected date into datepicker also

        }
    }