본문 바로가기

Android

날짜와 시간 - DatePicker

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" >

 

    <DatePicker

        android:id="@+id/datepicker"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:calendarViewShown="false"

        android:spinnersShown="true"/>

 

</LinearLayout>

 

MainActivity.java

 

package com.example.datepickerex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.widget.DatePicker;

import android.widget.Toast;

 

public class MainActivity extends Activity {

     DatePicker datePicker;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           datePicker = (DatePicker) findViewById(R.id.datepicker);

 

           datePicker.init(datePicker.getYear(),

                           datePicker.getMonth(),

                           datePicker.getDayOfMonth(),

                            new DatePicker.OnDateChangedListener() {

 

                           @Override

                           public void onDateChanged(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();

                           }

                     });

     }

 

     @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;

     }

 

}




 * DatePicker        

        android:calendarViewShown="true"



        android:spinnersShown="true"




'Android' 카테고리의 다른 글

날짜와 시간 - Chronometer  (0) 2013.04.02
날짜와 시간 - DatePickerDialog,TimePickerDialog  (0) 2013.04.02
래이팅바(RatingBar)  (0) 2013.04.02
시크바(SeekBar)  (0) 2013.04.02
프로그레스 바 (ProgressBar) 실습2  (0) 2013.04.02