あなたが言ったWhats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?
ユーザーが入力した日付形式の検証をチェックする必要がないかもしれない方法が 1 つあります。ボックスをクリックすると、 DatePickerDialogを呼び出すことができEditText
ます。その後、ユーザーはそれを使用して日付を選択できます。ユーザーが日付を選択したら、選択した日付で EditText を更新できます。このようにして、入力された日付形式の検証の労力が軽減され、ユーザーは簡単かつ直感的に日付を選択できます。あなたは次のようなものかもしれません:
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
//When the editText is clicked then popup the DatePicker dialog to enable user choose the date
edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(new_split.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
// Call this whn the user has chosen the date and set the Date in the EditText in format that you wish
private void updateLabel() {
String myFormat = "MM/dd/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}
ソース: Datepicker に関するこの回答: how to popup datepicker when click on edittext question. お役に立てれば。