Please suggest me some tutorial which gives the example for DatePicker and how to use its methods like O开发者_运维问答nDateChangedListener, onDateChanged etc. Actually I am going through some sites, but i did not get the clear idea of it.
Thank you
Android references on DatePicker is quite good. Have a look at it here.
private DatePicker datePicker;
//monthofYear is between 0-11
datePicker.init(2010, 11, 1, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
// Notify the user.
}
});
See.Example(); here
Check this Data Picker example: Example of DATE PICKER .
Step 1 : create a java file:
package com.example.babs;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.app.FragmentManager;
public class EditUserInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_edit_view);
}
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
// pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- ----- ---- ---- -----
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
FragmentManager fragmentManager = getFragmentManager();
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(fragmentManager, "datePicker");
}
}// end main class EditUserInfo
step 2: your xml file must contain :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true" >
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
You can try this code:
public
static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit.setText(day + "/" + (month + 1) + "/" + year);
}
}
Taken from Example of DatePickerFragment and TimePickerFragment.
精彩评论