0

ユーザーが日付のみ (時間なし) を入力する textedit フィールドを作成したいと考えています。に日付が格納されMY SQLます。最小限の検証でこれを行う最善の方法は何ですか? 日付を適切な形式に保つ組み込みのテキストフィールドのようなものはありますか?

私はこれを持っています:

public static void AddEditTextDate(Context context, LinearLayout linearlayout, String text, int id) {
    EditText edittext = new EditText(context);
    edittext.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
    edittext.setText(text);
    edittext.setId(id);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    edittext.setLayoutParams(params);
    linearlayout.addView(edittext);
}

でも入力してみると、普通のキーボードのように見えます。デフォルトでテンキーか何かに入ると思います...

編集:Android 2.1+(つまりv7)で動作する必要があります

誰か知っていますか?

ありがとう

4

1 に答える 1

2

あなたが言った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. お役に立てれば。

于 2013-07-27T22:07:17.820 に答える