DatePickerDialog

DatePickerDialog
workspace/DatePicker/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startdatepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start DatePicker"
/>
</LinearLayout>


workspace/DatePicker/src/com/DatePicker/DatePicker.java
package com.DatePicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DatePicker extends Activity {

private int year, month, day;
static final int ID_DATEPICKER = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button startDatePicker = (Button)findViewById(R.id.startdatepicker);
startDatePicker.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}});
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_DATEPICKER:
return new DatePickerDialog(this, dateSetListener, year, month, day);
default:
return null;
}
}

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

@Override
public void onDateSet(android.widget.DatePicker arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(),
String.valueOf(arg1) + "/"
+ String.valueOf(arg2+1) + "/"
+ String.valueOf(arg3),
Toast.LENGTH_LONG).show();
}
};
}

0 Response to "DatePickerDialog"

Posting Komentar