如何从DatePickerDialog更新EditText值

时间:2014-10-06 10:40:53

标签: android android-edittext android-datepicker

我有EditText已经有一些价值,当我点击EditText时,它会打开DatePickerDialog。所以我可以选择一些日期。一旦我选择了日期并单击完成,则应更新先前的值。但我不知道如何更新价值。

这是我的代码。

estimate_closedate.setText("2014-09-12");
estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("datepicker inside");
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }
estimate_closedate.setText(nextdate);
private DatePickerDialog.OnDateSetListener mDateSetListener = new 
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    myear=year;
                    mmonth=monthOfYear+1;
                    mday=dayOfMonth;
                    if(mmonth<10)
                    {
                        month = 0 + Integer.toString(mmonth);
                    }
                    else
                    {
                        month = Integer.toString(mmonth);
                    }
                    if(mday<10)
                    {
                        day = 0 + Integer.toString(mday);
                    }
                    else
                    {
                        day = Integer.toString(mday);
                    }
                    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

                }
            };

4 个答案:

答案 0 :(得分:1)

您忘了在EditText方法中更新onDateSet,只需将这一行放到最后:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    ///....       
    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("").append(day)).toString();

    estimate_closedate.setText(nextdate);//this is how you update edit text
}

答案 1 :(得分:0)

试试这个:

estimate_closedate.setText("2014-09-12");
estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("datepicker inside");
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }

private DatePickerDialog.OnDateSetListener mDateSetListener = new 
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    myear=year;
                    mmonth=monthOfYear+1;
                    mday=dayOfMonth;
                    if(mmonth<10)
                    {
                        month = 0 + Integer.toString(mmonth);
                    }
                    else
                    {
                        month = Integer.toString(mmonth);
                    }
                    if(mday<10)
                    {
                        day = 0 + Integer.toString(mday);
                    }
                    else
                    {
                        day = Integer.toString(mday);
                    }
                    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();
                    estimate_closedate.setText(nextdate);
                }
            };

答案 2 :(得分:0)

只需在日期选择器的onDateSet方法中设置EditText文本即可。 就在这一行之后:

            nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

添加:

estimate_closedate.setText(nextdate);

答案 3 :(得分:0)

试试这个:

 private DatePickerDialog.OnDateSetListener mDateSetListener = new 
        DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                myear=year;
                mmonth=monthOfYear+1;
                mday=dayOfMonth;
                if(mmonth<10)
                {
                    month = 0 + Integer.toString(mmonth);
                }
                else
                {
                    month = Integer.toString(mmonth);
                }
                if(mday<10)
                {
                    day = 0 + Integer.toString(mday);
                }
                else
                {
                    day = Integer.toString(mday);
                }
                nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

                estimate_closedate.setText(nextdate);
            }
        };