본문 바로가기

Android

날짜와 시간 - DatePickerDialog,TimePickerDialog

Activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/btn1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="날짜 다이얼로그 보이기" />

   

     <Button

        android:id="@+id/btn2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="시간 다이얼로그 보이기" />

 

</LinearLayout>

 

 

 

MainActivity.java

 

package com.example.datepickerex02;

 

import java.util.Calendar;

 

import java.util.GregorianCalendar;

import android.os.Bundle;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.TimePickerDialog;

import android.view.Menu;

import android.view.View;

import android.widget.DatePicker;

import android.widget.TimePicker;

import android.widget.Toast;

 

public class MainActivity extends Activity {

     int year, month, day, hour, minute;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

    

           GregorianCalendar calendar = new GregorianCalendar();

           year = calendar.get(Calendar.YEAR);

           month = calendar.get(Calendar.MONTH);

           day= calendar.get(Calendar.DAY_OF_MONTH);

           hour = calendar.get(Calendar.HOUR_OF_DAY);

           minute = calendar.get(Calendar.MINUTE);

    

           findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     new DatePickerDialog(MainActivity.this, dateSetListener, year, month, day).show();

                }

           });

           findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     new TimePickerDialog(MainActivity.this, timeSetListener, hour, minute, false).show();

                }

           });

     }

    

    

    

     private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

          

           @Override

          public void onDateSet(DatePicker view, int year, int monthOfYear,

                     int dayOfMonth) {

                // TODO Auto-generated method stub

        String msg = String.format("%d / %d / %d", year,monthOfYear+1, dayOfMonth);

        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

           }

     };

    

     private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {

          

           @Override

          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                // TODO Auto-generated method stub

               String msg = String.format("%d / %d / %d", year, hourOfDay, minute);

                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

           }

     };

    

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}





 

'Android' 카테고리의 다른 글

기타위젯 - AutoCompleteTextView  (0) 2013.04.02
날짜와 시간 - Chronometer  (0) 2013.04.02
날짜와 시간 - DatePicker  (0) 2013.04.02
래이팅바(RatingBar)  (0) 2013.04.02
시크바(SeekBar)  (0) 2013.04.02