본문 바로가기

나의 플랫폼/안드로이드

[Android][CustomView] Button with TimePickerDialog

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Button을 클릭 하면 TimePickerDialog를 보여주고, 시간을 선택 하면 Button 텍스타가 바뀌는 Custom UI를 만들어 보았습니다.


## TimePickerButton.java

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ryuandjo.customviews.widgets;
 
import android.app.TimePickerDialog;
import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
 
import com.ryuandjo.customviews.R;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
 
/**
 * Created by RyuChungGuen on 2017-09-27.
 */
 
public class TimePickerButton extends AppCompatButton implements TimePickerDialog.OnTimeSetListener{
    private TimePickerDialog.OnTimeSetListener mTimeSetListener;
    private TimePickerDialog mDialog;
    private String mDateFormat;
 
    public TimePickerButton(Context context) {
        super(context);
        initView();
    }
 
    public TimePickerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }
 
    public TimePickerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }
 
    private void initView(Context context, AttributeSet attrs) {
        initView();
 
        mDateFormat = context.obtainStyledAttributes(attrs, R.styleable.TimePickerButton).getString(R.styleable.TimePickerButton_dateFormat);
    }
 
    private void initView() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showTimePickerDialog();
            }
        });
    }
 
    public void setTimeSetListener(TimePickerDialog.OnTimeSetListener mTimeSetListener) {
        this.mTimeSetListener = mTimeSetListener;
    }
 
    private void showTimePickerDialog() {
        releaseTimePickerDialog();
 
        Calendar now = Calendar.getInstance();
        mDialog = new TimePickerDialog(getContext()
                , this
                , now.get(Calendar.HOUR_OF_DAY)
                , now.get(Calendar.MINUTE)
                , false);
        mDialog.show();
    }
 
    private void releaseTimePickerDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
 
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, i);
        calendar.set(Calendar.MINUTE, i1);
 
        if (TextUtils.isEmpty(mDateFormat)) {
            DateFormat format = SimpleDateFormat.getTimeInstance();
            setText(format.format(calendar.getTime()));
        } else {
            DateFormat format = new SimpleDateFormat(mDateFormat, Locale.getDefault());
            setText(format.format(calendar.getTime()));
        }
 
        if (this.mTimeSetListener != nullthis.mTimeSetListener.onTimeSet(timePicker, i, i1);
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        releaseTimePickerDialog();
    }
}
cs

## attrs.xml

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TimePickerButton">
        <attr name="dateFormat" format="reference|string" />
    </declare-styleable>
</resources>
cs


## MainActivity.java

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
package com.ryuandjo.customviews;
 
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
 
import com.ryuandjo.customviews.widgets.TimePickerButton;
 
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        TimePickerButton timePickerButton = (TimePickerButton) findViewById(R.id.btn_timepicker);
        timePickerButton.setTimeSetListener(this);
    }
 
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
 
    }
}
 
cs

## activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ryuandjo.customviews.MainActivity">
 
    <com.ryuandjo.customviews.widgets.TimePickerButton
        android:id="@+id/btn_timepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00:00"
        app:dateFormat="a : hh시 mm분"/>
 
</android.support.constraint.ConstraintLayout>
 
cs


git :: https://github.com/gorchg/CustomViews/blob/master/app/src/main/java/com/ryuandjo/customviews/widgets/TimePickerButton.java


위 소스에 대해 궁금한 사항이나 추가할만한 사항이 있으시면 언제든지 댓글 주세요. 만들어 볼께요~