반응형
데이터 객체 와 타임 객체를 하나의 복합객체를 만드는 법을 학습 하므로써 시간 과 날짜 객체 사용법을 익히고 정리하기 위해서 작성해 본다.
1) Date picker 와 Time picker 의 xml 레이 아웃을 정의 한다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<DatePicker
android:id="@+id/dataPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</DatePicker>
<CheckBox
android:id="@+id/checkTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시간 선택"
/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TimePicker>
</LinearLayout>
2) PicerView 를 만든다. 다음과 같은 기본 메소드를 오버라이드 한후, init 메소드를 만든다.
public class PickerView extends LinearLayout {
DatePicker datePicker;
TimePicker timePicker;
CheckBox checkTimePicker;
OnDateTimeChangeListener listener;
public PickerView(Context context) {
super( context );
init (context);
}
public PickerView(Context context, AttributeSet attrs) {
super( context, attrs );
init (context);
}
3) init 메소드 에서 다음과 같이 inflate 과정을 거친후 객체를 찾아준다.
private void init(Context context){
LayoutInflater inflater= (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate( R.layout.picker_time, this, true );
datePicker = (DatePicker) findViewById( R.id.dataPicker );
timePicker = (TimePicker) findViewById( R.id.timePicker );
checkTimePicker = (CheckBox) findViewById( R.id.checkTimePicker );
4) Date 객체를 생성 한다. 그리고 캘린더 객체를 만든후 현재 값을 셋팅 한다.
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime( date );
int curYear = calendar.get( Calendar.YEAR );
int curMonth = calendar.get(Calendar.MONTH);
int curDay = calendar.get( Calendar.DAY_OF_MONTH );
int curHour = calendar.get( Calendar.HOUR_OF_DAY );
int curMin = calendar.get( Calendar.MINUTE );
5) OnDateTimeChangeListener 를 다음과 같이 설정 한다.
public interface OnDateTimeChangeListener {
public void onDataTimeChange(PickerView view, int year, int month, int day, int hour, int min);
}
6) 다시 PickerView 로 돌아와서, init 바깥 부분에다가 setDateTimeChangeListener 를 설정 해주고 updateDateTime도 설정 해준다.
public void setOnDateTimeChangeListener(OnDateTimeChangeListener listener){
this.listener = listener;
}
public void updateDateTime(int year, int month , int day, int hour, int min){
datePicker.updateDate( year,month,day );
timePicker.setCurrentHour( hour );
timePicker.setCurrentMinute( min );
}
7) init 안 하단에다가 다음과 같이 넣는다.
datePicker.init( curYear, curMonth, curDay, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (listener != null) {
listener.onDataTimeChange(PickerView.this, year,monthOfYear,dayOfMonth,timePicker.getCurrentHour(),timePicker.getCurrentMinute() );
}
}
} );
timePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
if (listener != null) {
listener.onDataTimeChange(PickerView.this,datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),hourOfDay,minute );
}
}
} );
checkTimePicker.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
timePicker.setEnabled( isChecked );
timePicker.setVisibility( checkTimePicker.isChecked()? View.VISIBLE : View.INVISIBLE );
}
} );
8) MainActivity 에서 다음과 같이 datePicker 를 만들어 준다.
public class MainActivity extends AppCompatActivity {
PickerView pickerView;
TextView textView;
SimpleDateFormat format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
pickerView = (PickerView) findViewById( R.id.picker );
textView = (TextView) findViewById( R.id.textView );
format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
pickerView.setOnDateTimeChangeListener( new OnDateTimeChangeListener() {
@Override
public void onDataTimeChange(PickerView view, int year, int month, int day, int hour, int min) {
Calendar calendar = Calendar.getInstance();
calendar.set( year,month,day,hour,min );
String curTime =format.format( calendar.getTime() );
textView.setText( curTime );
}
} );
Button button = (Button) findViewById( R.id.button );
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
String curTime =format.format( calendar.getTime() );
int year = calendar.get( Calendar.YEAR );
int mon = calendar.get( Calendar.MONTH );
int day = calendar.get( Calendar.DAY_OF_MONTH );
int hour = calendar.get( Calendar.HOUR_OF_DAY );
int min = calendar.get( Calendar.MINUTE);
pickerView.updateDateTime( year,mon,day,hour,min );
}
} );
}
}
9) PickView 전체 code
public class PickerView extends LinearLayout {
DatePicker datePicker;
TimePicker timePicker;
CheckBox checkTimePicker;
OnDateTimeChangeListener listener;
public PickerView(Context context) {
super( context );
init (context);
}
public PickerView(Context context, AttributeSet attrs) {
super( context, attrs );
init (context);
}
private void init(Context context){
LayoutInflater inflater= (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate( R.layout.picker_time, this, true );
datePicker = (DatePicker) findViewById( R.id.dataPicker );
timePicker = (TimePicker) findViewById( R.id.timePicker );
checkTimePicker = (CheckBox) findViewById( R.id.checkTimePicker );
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime( date );
int curYear = calendar.get( Calendar.YEAR );
int curMonth = calendar.get(Calendar.MONTH);
int curDay = calendar.get( Calendar.DAY_OF_MONTH );
int curHour = calendar.get( Calendar.HOUR_OF_DAY );
int curMin = calendar.get( Calendar.MINUTE );
datePicker.init( curYear, curMonth, curDay, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (listener != null) {
listener.onDataTimeChange(PickerView.this, year,monthOfYear,dayOfMonth,timePicker.getCurrentHour(),timePicker.getCurrentMinute() );
}
}
} );
timePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
if (listener != null) {
listener.onDataTimeChange(PickerView.this,datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),hourOfDay,minute );
}
}
} );
checkTimePicker.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
timePicker.setEnabled( isChecked );
timePicker.setVisibility( checkTimePicker.isChecked()? View.VISIBLE : View.INVISIBLE );
}
} );
}
public void setOnDateTimeChangeListener(OnDateTimeChangeListener listener){
this.listener = listener;
}
public void updateDateTime(int year, int month , int day, int hour, int min){
datePicker.updateDate( year,month,day );
timePicker.setCurrentHour( hour );
timePicker.setCurrentMinute( min );
}
}
10) 동작 확인