Android 날짜 와 시간의 사용 실례 상세 설명

2630 단어 Android날짜.시간.
Android 날짜 와 시간의 사용

날짜 와 시간의 사용;
1:팝 업 상자 TimePickerDialog,DatePickerDialog
2:구성 요소 TimePicker,DatePicker
TimePickerDialog 사용:button 을 누 르 면 그림 1 을 표시 하고 시간 을 설정 할 수 있 습 니 다.
DatePickerDialog 의 사용 은 TimePickerDialog 를 DatePickerDialog 로 수정 하고 TimePickerDialog.OnTimeSetListener 는 각각 DatePickerDialog 로 수정 하 며 OnDateSetListener 도 가능 합 니 다.

public static class TimePickerFragment extends DialogFragment
              implements TimePickerDialog.OnTimeSetListener {
    //            
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
  }

  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
  }
}
 
  

 android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:text="@string/pick_time" 
  android:onClick="showTimePickerDialog" />

public void showTimePickerDialog(View v) {
  DialogFragment newFragment = new TimePickerFragment();
  newFragment.show(getSupportFragmentManager(), "timePicker");
}


DatePickerDialog 의 코드:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

  //              
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
    return dialog;
  }



  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {



  }

}

읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기