[안드로이드] Timepicker(타임피커), Datepicker(데이트피커) 예제
<복합위젯 - 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());
}
}
}
'안드로이드 > 레이아웃(Layout)' 카테고리의 다른 글
[Android] 안드로이드 - 프레임레이아웃 (FrameLayout) (0) | 2021.11.30 |
---|---|
[Android] 안드로이드 - 랠러티브 레이아웃(Relative Layout) (0) | 2021.11.30 |
[Android] 안드로이드 - 리니어 레이아웃 (Linear Layout) (0) | 2021.11.30 |
[안드로이드] 애니메이션 효과를 이용한 페이지 슬라이딩 (0) | 2018.01.17 |
[Android] Strings.xml에서 개행하기 (0) | 2018.01.17 |
[Android] 안드로이드 액션바 색상/디자인 변경하기(Action Bar Color Change/Action Bar Customize) (0) | 2018.01.17 |
안드로이드 상단 회색(그레이) 색상 타이틀바 없애기 또는 레이블 넣기 (0) | 2018.01.17 |
리스트뷰의 주요 속성 살펴보기 (0) | 2018.01.17 |