2013-07-27 2 views
0

사용자가 날짜 만 입력하는 textedit 필드를 만들고 싶습니다 (시간이 없음). 날짜는 MY SQL에 저장됩니다. 최소 유효성 검사로이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 적절한 형식으로 유지하는 날짜에 대한 텍스트 필드가 내장되어 있습니까?안드로이드에 프로그래밍 방식으로 만 날짜에 대한 textedit를 만드는 방법은 무엇입니까?

나는이 있습니다

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); 
} 

을하지만 그 안에 입력하려고 할 때, 그것은 일반 키보드처럼 보인다. 나는 ... 그것은 기본 또는 무언가에 의해 숫자 패드로 이동합니다

편집을 기대 : 그것은 안드로이드 2.1 (즉, V7)

사람이 알고 있나요 작업 할 필요가 있겠습니까?

감사

+0

필자는이 파일을 일부 XML 파일에 정의하고 필요한 경우로드하는 것이 좋습니다. 숫자/전화 번호 등 다른 입력 유형을 테스트하십시오. –

+0

사용자가 데이터를 변경할 수 있기 때문에 동적으로 삽입해야합니다. – sneaky

+1

숫자 형 키보드를 표시하려면'InputType.TYPE_DATETIME_VARIATION_DATE' 대신'InputType.TYPE_CLASS_DATETIME'을 사용하십시오. 물론 사용자 입력 후 날짜와 형식을 확인해야합니다. 당신은'regex'를 사용할 수 있습니다. – Vikram

답변

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?

말했다. EditText 상자를 클릭하면 DatePickerDialog 번으로 전화 할 수 있습니다. 그런 다음 사용자는이를 사용하여 날짜를 선택할 수 있습니다. 사용자가 날짜를 선택하면 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 질문에 This 대답. 희망이 도움이됩니다.

+0

android 2.1에서 작동해야합니다. – sneaky

+0

DatePicker 클래스 자체는 API 레벨 1부터 있습니다. AlertDialog.Builder를 사용하여 대화 상자를 만들고 내용보기를 DatePicker 인스턴스로 설정할 수 있습니다. (또는 ActionBarSherlock을 사용하면 SherlockDialogFragment를 사용하고 거기에 DatePicker를 넣을 수 있습니다.) – Karakuri

+0

@ Karakuri가 옳습니다. 당신은 안드로이드 2.1에서 이것을 사용할 수 있습니다. 그가 올바르게 언급 한대로 ActionBarSherlock에 갈 수도 있습니다. –

관련 문제