티스토리 뷰

반응형

원하는 년도만 선택할 수 있는 다이얼로그가 필요했다.

DatePicker를 사용하여 월, 일을 제거하고 사용하는 방법도 있지만 NumberPicker로 구성해 보았다.

캘린더에서 년도를 가져오고 min, max를 지정하고 기본 디폴트 값을 지정한다.

다음은 관련 코드 전부이다. 리소스 연동은 필요여하에 따라 변경하거나 삭제하면 된다.


선언부

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
private void showBirthDayPicker() {
    Calendar calender = Calendar.getInstance();
    int year = calender.get(Calendar.YEAR);
 
    final Dialog birthdayDialog = new Dialog(this);
    birthdayDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    birthdayDialog.setContentView(R.layout.dialog_birthday);
 
    Button okBtn = (Button) birthdayDialog.findViewById(R.id.birthday_btn_ok);
    Button cancelBtn = (Button) birthdayDialog.findViewById(R.id.birthday_btn_cancel);
 
    final NumberPicker np = (NumberPicker) birthdayDialog.findViewById(R.id.birthdayPicker);
    np.setMinValue(year - 100);
    np.setMaxValue(year - 15);
    np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    setDividerColor(np, android.R.color.white );
    np.setWrapSelectorWheel(false);
    np.setValue(year-20);
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            
        }
    });
    okBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editBirthday.setText(String.valueOf(np.getValue()));
            birthdayDialog.dismiss();
        }
    });
    cancelBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            birthdayDialog.dismiss();
        }
    });
 
    birthdayDialog.show();
}
 
 
private void setDividerColor(NumberPicker picker, int color) {
    java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
    for (java.lang.reflect.Field pf : pickerFields) {
        if (pf.getName().equals("mSelectionDivider")) {
            pf.setAccessible(true);
            try {
                ColorDrawable colorDrawable = new ColorDrawable(color);
                pf.set(picker, colorDrawable);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}
 
cs

타이틀 제거 코드는 setContentView()함수 호출 전에 선언해주어야 한다.

birthdayDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


피커 가운데 나오는 라인 제거 

setDividerColor() 호출


데이터 선택 시 edittext 방지.

np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);




dialog.xml코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 
 
    <LinearLayout
        android:id="@+id/birthday_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/WHITE"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:visibility="visible" >
 
        <TextView
            android:id="@+id/birthday_dialog_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:minHeight="42dp"
            android:text="출생년도를 선택해주세요."
            android:visibility="visible"
            android:textSize="@dimen/small"
            android:textColor="@color/content_first_color" />
 
        <NumberPicker
            android:id="@+id/birthdayPicker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
 
            <Button
                android:id="@+id/birthday_btn_cancel"
                android:layout_weight="1"
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_below="@+id/birthdayPicker"
                android:text="취소" />
 
            <Button
                android:id="@+id/birthday_btn_ok"
                android:layout_weight="1"
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="확인" />
        </LinearLayout>
 
 
    </LinearLayout>
</RelativeLayout>
 
 
cs



최종 결과 화면




반응형
댓글
반응형