본문 바로가기

카테고리 없음

DatePickerDlg

MainActivity.java

public class MainActivity extends AppCompatActivity  {
    Button btnSelectDate, btnSelectTime;
    DatePickerDialog datePickerDialog;
    TimePickerDialog timePickerDialog;
    TextClock tClock;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSelectDate=(Button)findViewById(R.id.btnSelectDate);
        btnSelectTime=(Button)findViewById(R.id.btnSelectTime);
        tClock = findViewById(R.id.textClock);
        textView = findViewById(R.id.textView);
    }

    public void onClick(View v) {
        if (v == btnSelectDate) {
            final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR);
            int mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                            btnSelectDate.setText(year + " / "+ (monthOfYear+1) +" / " + dayOfMonth );
                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == btnSelectTime) {
            final Calendar c = Calendar.getInstance();
            int mHour = c.get(Calendar.HOUR_OF_DAY);
            int mMinute = c.get(Calendar.MINUTE);

            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {
                            btnSelectTime.setText(String.format("%02d:%02d", hourOfDay, minute));
                            textView.setText("클릭한시간: "+tClock.getText() );
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.show();
        }
    }
}

activiy_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextClock
        android:id="@+id/textClock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#80CC28"
        android:textSize="45dp"
        android:textStyle="bold"
        android:format12Hour="hh:mm:ss a" />

    <Button
        android:id="@+id/btnSelectDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="날짜 설정" />

    <Button
        android:id="@+id/btnSelectTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="시간 설정" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:text="TextView" />


</LinearLayout>