[안드로이드] Timepicker(타임피커), Datepicker(데이트피커) 예제

2018. 1. 17. 19:57 안드로이드/레이아웃(Layout)

<복합위젯 - Timepicker, Datepicker>

* 타임피커(Timepicker) - 시간 선택 위젯

실습1 - timepicker를 이용해 설정된 시각 출력



 

1) activity_timepicker.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textSize="20dp"
        android:textColor="#3A69D3"/>


    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tp"
        android:layout_below="@id/tv"
        android:timePickerMode="spinner" />


</RelativeLayout>

 

2) TimePickerDemo.java

public class TimePickerDemo extends AppCompatActivity implements TimePicker.OnTimeChangedListener{


    TextView tv;
    TimePicker tp;
    Calendar c;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timepicker);
        c = Calendar.getInstance(); //캘린더 객체 생성
        int hourofDay = c.get(c.HOUR_OF_DAY); //현재 시
        int minute = c.get(c.MINUTE); //현재 분
        int second = c.get(c.SECOND); //현재 초
        tv = (TextView) findViewById(R.id.tv);
        tp = (TimePicker) findViewById(R.id.tp);
        tv.setText("초기 설정된 시각 \n시:분:초 | "
                + hourofDay + ":" + minute + ":" + second);
        tp.setOnTimeChangedListener(this);
    }


    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        tv.setText("현재 설정된 시간 \n시:분 | " + hourOfDay + ":" + minute);
    }
}

 

실습2 - TimePickerDialog를 사용하여 시간 설정

 

1) activity_timepicker2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:onClick="onBtnClicked"
        android:text="Set time"
        android:layout_below="@id/tv"/>


</RelativeLayout>

 

2) TimePickerFragment.java

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {


        //현재 시간을 타임 피커의 초기값으로 사용
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);


        TimePickerDialog tpd = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_DARK, this,
                hour, minute, DateFormat.is24HourFormat(getActivity()));


        //타임 피커의 타이틀 설정
        TextView tvTitle = new TextView(getActivity());
        tvTitle.setText("TimepickerDialog 타이틀");
        tvTitle.setBackgroundColor(Color.parseColor("#ffEEE8AA"));
        tvTitle.setPadding(5, 3, 5, 3);
        tvTitle.setGravity(Gravity.CENTER_HORIZONTAL);
        tpd.setCustomTitle(tvTitle);
        return tpd;
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        TextView tv = (TextView) getActivity().findViewById(R.id.tv);
        String aMpM = "AM";
        if(hourOfDay > 11){
            aMpM = "PM";
        }
        int currentHour;
        if(hourOfDay > 11){
            currentHour = hourOfDay-12;
        }
        else{
            currentHour = hourOfDay;
        }


        tv.setText("설정된 시간은.. \n\n");
        tv.setText(tv.getText() + String.valueOf(hourOfDay) + "시 "
                + String.valueOf(minute) + "분\n");
    }
}

 

3) TimePickerDemo2.java

public class TimePickerDemo2 extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timepicker2);
    }


    public void onBtnClicked(View v){
        TimePickerFragment newFragment = new TimePickerFragment();
        //show : fragmentManager에 추가된 대화상자 출력
        newFragment.show(getFragmentManager(), "TimePicker");
    }
}

 

실습3 - 날짜선택위젯 Datepicker

 

 

1) activity_datepicker

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textSize="20dp"
        android:textColor="#CF446F"/>


    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dp"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"
        android:layout_below="@id/tv" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_update_date"
        android:text="날짜 업데이트"
        android:layout_below="@id/dp"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_get_date"
        android:text="날짜 읽어오기"
        android:layout_below="@id/dp"
        android:layout_toRightOf="@id/btn_update_date"/>


</RelativeLayout>

 

2) DatePickerDemo.java

public class DatePickerDemo extends AppCompatActivity implements View.OnClickListener {
    TextView tv;
    DatePicker dp;
    Calendar c;
    Button btnUpdateDate;
    Button btnGetDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datepicker);


        c = Calendar.getInstance();
        int year = c.get(c.YEAR);
        int month = c.get(c.MONTH); //1월은 '0'부터 시작
        int dayOfMonth = c.get(c.DAY_OF_MONTH);


        tv = (TextView) findViewById(R.id.tv);
        btnUpdateDate = (Button) findViewById(R.id.btn_update_date);
        btnGetDate = (Button) findViewById(R.id.btn_get_date);
        dp = (DatePicker) findViewById(R.id.dp);


        tv.setText("초기 설정된 날자 [월/일/년도] : \n"
                + year + "/" + (month+1) + "/" + dayOfMonth);
        btnUpdateDate.setOnClickListener(this);
        btnGetDate.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(v == btnUpdateDate) {
            tv.setText("");
        }
        else{
            tv.setText("선택된 날짜 : [월/일/년도]\n");
            tv.setText(tv.getText() + " " + dp.getYear() + "/"
                + (dp.getMonth()+1) + "/" + dp.getDayOfMonth());
        }
    }
}

출처 : http://blog.naver.com/kcwwck77/220561789931