开发者

inline datepicker in android

I am developing an android app in which I have to collect date of birth in a form So I used a textview with a datepicker as shown below

inline datepicker in android

I want to pi开发者_StackOverflow中文版ck date from it and store it in form of ints like month,day,year. I tried many examples but all deal with dialog box. None of them showed using the datepicker inline :-( Please kindly help me out.


Just use the getYear(), getMonth() and getDayOfMonth() methods:

DatePicker datePicker = (DatePicker) findViewById(R.id.my_date);    

int year = datePicker.getYear();
int month = datePicker.getMonth();
int day = datePicker.getDayOfMonth();


In the below example I attached the functionality of change the date onClick of the EditText. Similarly, if you have a button to save you can attache this functionality to save the date in the required format. Just to get the date, month and year use the below methods:

datePicker.getYear(),getMonth(), getDayOfMonth().

Note that you need to add 1 to get the correct month as Java Month representation is a little different. the mapping goes like this - 0-Jan, 1-Feb ... 11-Dec.

private TextView mDateDisplay;
private DatePicker mDatePicker;
private int mYear;
private int mMonth;
private int mDay;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    mDateDisplay = (TextView)findViewById(R.id.datevalue);
    mDatePicker = (DatePicker)findViewById(R.id.datepicker1);
    mDateDisplay.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            mYear = mDatePicker.getYear();
            mMonth = mDatePicker.getMonth();
            mDay = mDatePicker.getDayOfMonth();
            
            mDateDisplay.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(mMonth + 1).append("-")
            .append(mDay).append("-")
            .append(mYear).append(" "));
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜