再次选择日期时,如何在EditText中显示上次选择的日期?

时间:2014-02-19 06:39:03

标签: android android-datepicker

enter image description here以下是选择日期的来源。它工作正常但我想在再次选择日期时更改日期。

private int year;
private int month;
private int day;
         signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);
         signup_bday.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new       DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {                  
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                     signup_bday.setText(new StringBuilder().append(month +  1).append("-").append(day).append("-").append(year).append(" "));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.show();
        }
    });

enter image description here

2 个答案:

答案 0 :(得分:2)

好的,我解决了你的问题,这是代码,

public class MainActivity extends Activity 
{
    private int year;
    private int month;
    private int day;
    private EditText signup_bday;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);
        signup_bday.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, new OnDateSetListener()
                {                  
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                        year = selectedyear; 
                        month = selectedmonth; 
                        day = selectedday;
                        signup_bday.setText(new StringBuilder().append(month +  1).append("-").append(day).append("-").append(year).append(" "));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.show();
            }
        });
    }
}

您只需在onDateSet()方法中添加以下行,

year = selectedyear; 
month = selectedmonth; 
day = selectedday;

答案 1 :(得分:1)

我认为你的onDateSet方法没有被调用。添加@override注释并尝试使用代码。

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

}

我的理解......第一次显示当前日期。如果用户选择5,2014年1月,那么第二次它将显示先前选择的数据(5,2014年1月)。如果你想要实现这一目标,那么试试这样的事情

final Calendar cal = Calendar.getInstance();
try {
    cal.setTime(new Date(PREVIOUSLY_SELECTED_DATE));
} catch (IllegalArgumentException e) {

}